我不太明白这是怎么回事
fun foo() {
listOf(1, 2, 3, 4, 5).forEach {
if (it == 3) return // non-local return directly to the caller of foo()
print(it)
}
println("this point is unreachable")
}
fun main() {
foo()
}
输出为
12个
所以整数1
和2
.这是因为对于1
和2
,表达式if (it == 3)
的计算结果为false
.但return
号飞机在这里做什么呢?退货的是什么?go 哪里呢?有没有办法写得更冗长?