Kotlin: Why Break or Continue can’t be used in forEach or repeat, while for and while can use them

Warning: Parameter 2 to qtranxf_postsFilter() expected to be a reference, value given in /home/and-beyond/www/main/wp-includes/class-wp-hook.php on line 286

In Kotlin while and for block, break and continue can be used, but they can’t be used in forEach or repeat block.

Environment

• Kotlin 1.3.11

First, I will introdyce easy coping strategy when we want to use break or continue in forEach or repeat.

Easy Coping Strategy

Both forEach and repeat can be replaced with for. If we rewrite with for, then we can use break and continue.

 Before After forEach something.forEach for (it in something) repeat repeat (time) for (it in time)

Why break or continue can’t be used

(Directly speaking, Kotlin is built as it is.) while and for are completely different from forEach and repeat.

• while and for are syntax defined as Kotlin.
• forEach and repeat are functions

They are very similar in writing, but while and for are syntax, in which break and continue can be used. On the other hand, forEach and repeat are higher order functions defined in Kotlin standard library.

So,　{ ... } is function for forEach and repeat.

It is weird that break and continue appear in function, isn’t it? break and continue can’t be used.

Another way to control iteration

To escape the block in higher-order functions such as forEach and repeat, use return. It is natural that return finish it, because it is a function.

But, using return as it is, the caller function will be finished.

Use label to control it easily.

continue-like control

Using lambda

return@labelOfLambda is continue-like control in lambda.

The label can be named by ourselvs, such as labelName@{ ... }.

If double forEach are used

If forEach is used in double, name the label by yourself. In the following code, continue works for outer forEach loop.

Using anonymous function

If you change lambda to anonymous function, you can simply use return for continuation.

break-like control

Use label, but you need outer scope function.

Using lambda

Surround with run, and write return@run.

As you can see, lambda is built without consideration of break.

Using anonymous function

Use label like when we use lambda.

Summary of break and continue

How to use break and continue in forEach and repeat
 lambda anonymous function break continue Scope function and label label return