
[英]Declare a function generic type parameter in a way that allows both for nullable types and using that type parameter to declare `KClass<T>`
[英]How to check whether generic parameter T is nullable?
给定一个 function
inline fun <reified T> foo() {
//... do something differently if T is nullable ...
}
如何检查T
是否可以为空?
听起来很明显,一旦你知道它:
T
可以为空...如果null
可以是T
类型。
例如
inline fun <reified T> isTypeNullable(): Boolean = when {
null is T -> true
else -> false
}
(重要的是,这里T
是reified
,否则它将被删除并且整个事情都会引发异常)
问题未解决?试试以下方法:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.