~There is no `return`ing from lambdas~
# announcements
d
There is no `return`ing from lambdas
s
Sure you can. You just need to name it
d
Copy code
val x: (Int) -> Int = {
    // cannot return here
}
r
yes, you can
You can most certainly
return
from lambdas
Copy code
int foo = 4
foo.let{
  if(it != 4) {
    return@let
  }
  else {
    Timber.d("foo")
  }
}
is perfectly functioning code
d
You are returning from
let
, that is a normal function. You are not returning from the lambda.
r
You are technically correct, which is the best kind
d
Well... actually I am not correct as @spand just showed. I didn't know about labeling lambdas. Sorry. 🙈
h
AFAIK you are actually both correct –
return
won't work when you assign the lambda to a variable...
r
I’ve learned an important lesson today. Lambdas are hard, and I know less than Jon Snow
d
@hho Yes, they do:
Copy code
val x: (Int) -> Int = lit@{
    return@lit 13
}
Once again, the
return@let
thing shown above does not "return from the lambda", it returns early from let (which is an inline function).
h
Yeah, but isn't that the useful case? That's at least what I meant – you can't return from, say, a
foreach
with a lambda stored in a
val
.
d
You can return from the lambda just fine. What you cannot do is return from the function calling
forEach
.
h
You also can't return from forEach…
(which does work, if you don't assign it to a variable)
d
Yes, true.