'Breaking out from lambda using return@label not working in Kotlin for "string".chars()

Following code tries to break out from forEach lambda when ascii value of character is 59. It however does not compile.

fun foo() {
    run label@ {
        "abcd".chars().forEach {
            if (it == 59) return@label
            print(it)
        }
        print("completed forEach")
    }
}

However, it compiles if forEach is used directly on characters instead of calling chars() method to get ascii values as shown below

fun foo() {
    run label@ {
        "abcd".forEach {
            if (it == 'd') return@label
            print(it)
        }
        print("completed forEach")
    }
}


Solution 1:[1]

Following change worked

"abcd".chars().toArray().forEach {...}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 AdityaKapreShrewsburyBoston