Ayden
08/30/2018, 6:28 AM->
in lamdas as return?diesieben07
08/30/2018, 6:29 AMAyden
08/30/2018, 7:00 AM@lit
use for?@filter
edwardwongtl
08/30/2018, 7:02 AMreturn@filter
, making a return to another scopereturn
will only return from the current function scope.Ayden
08/30/2018, 7:06 AMedwardwongtl
08/30/2018, 7:07 AMNon-local return
Ayden
08/30/2018, 7:30 AMedwardwongtl
08/30/2018, 7:32 AMAyden
08/30/2018, 7:40 AMfun foo() {
run loop@{
listOf(1, 2, 3, 4, 5).forEach {
if (it == 3) return@loop // non-local return from the lambda passed to run
print(it)
}
}
print(" done with nested loop")
}
12 done with nested loop
fun foo() {
run {
listOf(1, 2, 3, 4, 5).forEach {
if (it == 3) return@forEach
// non-local return from the lambda passed to run
print(it)
}
}
print(" done with nested loop")
}
1245 done with nested loopedwardwongtl
08/30/2018, 7:43 AMreturn@forEach
actually work as continue
Ayden
08/30/2018, 8:46 AMcontinue
or return@forEach
right?
fun foo() {
listOf(1, 2, 3, 4, 5).forEach lit@{
if (it == 3) return@lit // local return to the caller of the lambda, i.e. the forEach loop
print(it)
}
print(" done with explicit label")
}
fun main(args: Array<String>) {
foo()
}
fun foo() {
run loop@{
listOf(1, 2, 3, 4, 5).forEach {
if (it == 3) return@loop // non-local return from the lambda passed to run
print(it)
}
} <- Here is the end of the local scope right?
print(" done with nested loop")
}
12 done with nested loop
is because the end of the local scope is after the close bracket of the run
.Shawn
08/30/2018, 1:32 PM