我有以下代码:
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
但是每个Kotlin类都具有Any作为超类,那么为什么会出现此错误?
T
在此函数中为空。您应该明确指定它不可为空。
fun <T : Any> test(t: T) = map.put(0, t)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句