Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type mismatch: inferred type is T but kotlin.Any was expected

Tags:

kotlin

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?

like image 786
netimen Avatar asked Apr 20 '16 13:04

netimen


1 Answers

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)
like image 161
Michael Avatar answered Nov 11 '22 09:11

Michael