我试过如下代码
val a: Int? = 1024
println(a is Int) // true
println(a is Int?) // true
println(a is String) // **error: incompatible types: Long and Int? why the value cannot be checked with any type?**
Run Code Online (Sandbox Code Playgroud)
但这很有效:
fun checkType(x: Any?) {
when(x) {
is Int -> ...
is String -> ... // **It works well here**
else -> ...
}
}
Run Code Online (Sandbox Code Playgroud) kotlin ×1