小编SK.*_*hen的帖子

kotlin 检查类型不兼容的类型

我试过如下代码

 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

6
推荐指数
1
解决办法
6257
查看次数

标签 统计

kotlin ×1