null pointer null pointer - 2 months ago 13
Android Question

the label dose does not denote a loop in foreach

I am continuing from a loop in Kotlin but I get the warning from the studio that the label dose does not denote a loop. could someone tell me whats wrong in the syntax?

here is the code segment

newRooms.forEach roomloop@ { wallRoom: WallRoom ->

val index = rooms.indexOf(wallRoom)

val room = rooms[index] //get the corresponding room.
//check if the last session is same in the room.

Answer Source

The labeled lambda expression here is a function literal, not a loop.

You cannot break or continue the lambda expression here because it is independent from the for-loop.

public inline fun <T> Array<out T>.forEach(action: (T) -> Unit): Unit {
    for (element in this) action(element)

You may use return to return from the function.


Note that the code segments below behave the same as the other one, both of them will print 123:

arrayOf(1, 2, 3).forEach label@ {

label@ for (i in arrayOf(1, 2, 3)) {