为什么我不能
return s?.let{ it }?: {throw IllegalStateException("you got something screwed up...")}
为了一个函数?根据我的理解,编译器应该能够检测到我们有非空或异常吗?
fun foo(): String{
val s: String? = null
return s?.let{ it }?: {throw IllegalStateException("you got something screwed up...")}
}
或
fun foo(): String{
val s: String? = null
s?.let{
return it
}?: {throw IllegalStateException("you got something screwed up...")}
}
编译器应该能够检测到我们有非空或异常
这不是你所拥有的:你返回一个抛出异常的
String
或function,特别是() -> Nothing
类型的函数。
您可能想退回这个:
return s?.let { it } ?: throw IllegalStateException("you got something screwed up...")
请注意,我删除了函数中包裹异常的大括号。