Karlo Lozovina
02/18/2022, 9:28 PM(1..10).forEach {
println(it)
return@forEach
}
print all of 10 numbers? what does even "return@forEach" mean here? It seems to work more like "continue", instead of "break"?bezrukov
02/18/2022, 9:49 PMfor (it in 1..10)
) or non-labeled return. It's impossible to get break behavior with forEach (you need to use something like takeWhile)Karlo Lozovina
02/18/2022, 9:56 PMephemient
02/18/2022, 9:57 PMval lambda: (Int) -> Unit = forEach@{
println(it)
return@forEach
}
for (i in 1..10) {
lambda(i)
}
which makes it clear why return
can't stop the forEach
Youssef Shoaib [MOD]
02/19/2022, 10:38 AMrun {
(1..10).forEach {
println(it)
if(it == 5){ //Break condition
return@run
}
}
}
Karlo Lozovina
02/19/2022, 1:58 PM