有一个Map<K, V>
,并且需要按键查找并将结果传递到仅包含null值的SDK的api。喜欢:
fun doSomeThing(value: V)
查找可能是
val v = theMap[key]
但v现在可以为空,即使保证此处使用的键存在并且v也不能为null。
有没有一种方法可以使函数在需要从地图查找的情况下不返回可为空的值?
fun returnNotNullable(key: K): V {
check(keyExist())
return theMap[key]!! // dont like the !! here
}
只是不想使用!!
像doSomeThing(theMap[key]!!)
做?: throw IllegalStateException("Item not found")
而不是doublebang。的类型T? ?: throw ...
将是不可为null的T
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句