At the fear of turning this into stackoverflow... ...
# random
d
At the fear of turning this into stackoverflow... Anyone know why the call mapType.keyType() does not compile here? https://git.io/v9VFY
stackoverflow 4
o
@diesieben07 I guess you need to specify
K
. Because there is no way to infer that from context.
d
Well, there is no way for me to specify it, because I don't know it. I am wondering why it does work with the elementType.
o
It has to do with variance. If you change the function's signature to the following, it should work:
fun <K, M : Map<out K, *>> TypeToken<M>.keyType(): TypeToken<K> {
(Note the
out
variance) The reason you don't have to do that with
elementType
is because
List
already declares its type parameter as covariant.
Map
does not do this, so you have to explicitly state that.
d
Thank you very much, that works!