I have the following code:
val map = HashMap<Int, Any>()
fun <T> test(t: T) = map.put(0, t) // Type mismatch: inferred type is T but kotlin.Any was expected
But every Kotlin class has Any as a superclass, so why this error?
T
is nullable in this function. You should explicitly specify that it's non-nullable.
fun <T : Any> test(t: T) = map.put(0, t)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With