
[英]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.