Kotlin has very nice iterating functions, like forEach
or repeat
, but I am not able to make the break
and continue
operators work with them (both local and non-local):
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.
The problem might be a bug with labels (M12), but I think that the first example should work anyway.
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)
}
}
Best Answer
This will print 1 to 5. The
return@forEach
acts like the keywordcontinue
in Java, which means in this case, it still executes every loop but skips to the next iteration if the value is greater than 5.This will print 1 to 10 but skips 5.
Try them at Kotlin Playground.