Non-Local and Labeled return

By default lambdas aren’t allowed to have the return keyword, even if they return a value. This is a significant difference between lambdas and anonymous functions—the latter is required to have return if returning values and it signifies only a return from the immediate lambda and not the outer calling function. In this section, we’ll look at why return is not allowed by default, how to use labeled return, and when non-local return is permitted.

return Not Allowed by Default

return is not valid in lambdas by default, but you can use it under some special situations. This can be quite confusing if you don’t take the time to fully understand the context and the consequences. Take this topic a bit slow for the details ...

Get Programming Kotlin now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.