Hi! I have: `interface Attributes<T> enum c...
# announcements
c
Hi! I have:
Copy code
interface Attributes<T>
enum class StringAttributes : Attributes<T> { ... }
val attributes: Map<Attributes<Any>, Any>
val others: Map<StringAttributes, String> = ...
However, if I write:
attributes = others
I get a
Type Mismatch
. I guess the solution has something to do with properly using in and out, however I don't understand what the correct way to use those is.
By trial and error, I found that this works:
interface Attributes<out T>
val attributes: Map<out Attributes<Any>, Any>
However, I'm not completely sure I understand why that works, even after reading the documentation...
s
This relates to type variance. This article explains is quite well. https://link.medium.com/SjF3tlITbcb One thing I like to always add when trying to explain sub-types, is relating sub-types to 'assignability': If
B
is a sub-type of
A
, then you can safely assign a variable of type
B
to a variable of type `A`:
Copy code
val aB: B = ....
val anA: A = B
.