Kotlin有非常好的迭代函数,比如forEach
或repeat
,但我无法让break
和continue
运算符与它们一起工作(本地和非本地):
repeat(5) {
break
}
(1..5).forEach {
continue@forEach
}
The goal is to mimic usual loops with the functional syntax as close as it might be. It was definitely possible in some older versions of Kotlin, but I struggle to reproduce the syntax.
问题可能是标签(M12)的错误,但我认为第一个示例应该可以工作.
It seems to me that I've read somewhere about a special trick/annotation, but I could not find any reference on the subject. Might look like the following:
public inline fun repeat(times: Int, @loop body: (Int) -> Unit) {
for (index in 0..times - 1) {
body(index)
}
}