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.
CLOVIS
12/13/2020, 6:23 PM
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
streetsofboston
12/13/2020, 7:41 PM
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