相关疑难解决方法(0)

为什么不能在 let 或 run 中使用 continue

为什么不允许continuefromlet函数?

此代码

fun foo(elements: List<String?>) {
    for (element in elements) {
        element?.let {
            continue  // error: 'break' or 'continue' jumps across a function or a class boundary
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

甚至这个代码

fun foo(elements: List<String?>) {
    loop@ for (element in elements) {
        element?.let {
            continue@loop  // error: 'break' or 'continue' jumps across a function or a class boundary
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

不编译错误:

'break' 或 'continue' 跳过函数或类边界


我知道在这种特殊情况下我可以使用filterNotNullsmart cast 或手动检查,但我的问题是为什么 …

kotlin

9
推荐指数
1
解决办法
245
查看次数

标签 统计

kotlin ×1