[英]Kotlin: Check for Null and Assign?
var something: Something? = null
val notNull: Something = ...
...
fun getSomething() {
something = something ?: notNull
return something
}
Kotlin 中是否有任何方法可以同时something = something?: notNull; return something
同时something = something?: notNull; return something
?
或者.. 更好的是,有没有办法在内联 function 中使用 generics?
fun getSomething(): Something {
return something ?: notNull.also { something = it }
}
// or
fun getSomething(): Something = something ?: notNull.also { something = it }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.