Anyone knows why I can’t do something like this? `...
# announcements
c
Anyone knows why I can’t do something like this?
Copy code
fun myFunction(list: List<String>): List<String> {
    return list.map {
        return it
    }
}
It complains about the return type being a string instead of a List<String>
d
return
is not used inside lambdas. In this case the
return
is returning from
myFunction
(the nearest function context). That works, because
map
is
inline
. In lambdas the last expression is the "return value".
c
So how can I achieve something like that? I was already thinking that the return was returning from the
myFunction
d
Copy code
return list.map {
  it
}
c
Gotcha! Thank you!
r
You can actually return here, but you need to specify from which context you want to return with labels
You can
return@myFunction
(equals to
return
in your case) or
return@map
(what you want). But it’s easier to just let the last expression be the returned value
m
I assume/hope the Lambda definition of
it
is a placeholder for something more complicated, right? Otherwise you could just return the original list.
c
Yeah, it was just placeholder to simplify my question 🙂
👍 2