fun checkLengthA(str : String?): Int = if (str.isNullOrBlank()) 0 else str.length
Run Code Online (Sandbox Code Playgroud)
“在 String 类型的可空接收器上只允许安全 (?.) 或非空断言 (!!.) 调用?
所有空对象(或空)都被 isNullOrBlank() 捕获,因此 str.length 中的 str 对象永远不能为空(或空)。这可以通过用显式检查替换扩展函数来实现。
fun checkLengthB(str : String?): Int = if (str == null) 0 else str.length
Run Code Online (Sandbox Code Playgroud)
或不那么冗长的表达:
fun checkLengthC(str : String?): Int = str?.length ?: 0
Run Code Online (Sandbox Code Playgroud)
checkLengthB 和 checkLengthC 都可以正常运行。
让我们从 checkLengthA 中删除可空类型以避免编译错误,如上所示:
fun checkLengthA(str : String): Int = if (str.isNullOrBlank()) 0 else str.length
Run Code Online (Sandbox Code Playgroud)
现在,我们只允许解析使用 String 类型的非空参数,所以如果我们期望一些空类型,那么我们必须把“?” 背部。
看起来编译器不明白在运行 str.length 和扩展函数时,String 类型的 str 永远不会计算为 null,但是如果我们在 if-else 语句中使用 (str …
kotlin ×1