Implicit return from lambda in Kotlin

前端 未结 1 1108
醉酒成梦
醉酒成梦 2020-12-09 16:30

It seems that the last line of a lambda always returns that value even if you omit the return statement. Is this correct? Is it documented anywhere?

<         


        
相关标签:
1条回答
  • 2020-12-09 16:58

    Yes, this is correct, if the last statement of a lambda is an expression, it is considered its return value.

    Here's what the reference says (thanks @KirillRakhman):

    We can explicitly return a value from the lambda using the qualified return syntax. Otherwise, the value of the last expression is implictly returned. Therefore, the two following snippets are equivalent:

    ints.filter {
        val shouldFilter = it > 0 
        shouldFilter
    }
    

    ints.filter {
        val shouldFilter = it > 0 
        return@filter shouldFilter
    }
    

    The last statement semantics is also true for if (that's why there's no ternary operator), when and try-catch blocks, and these statements are expressions themselves:

    val foo = if (bar) { 
        doSomething()
        baz 
    } else { 
        doSomethingElse()
        qux 
    }
    

    See also: examples for when and try-catch.

    So, lambdas are consistent with the language constructs in this respect.


    If you want to make an explicit return statement in a lambda, use the return@label syntax (also, another answer with examples). Non-labeled return, on contrary, works with the nearest fun (ignoring lambdas) and thus can only occur in those lambdas which are inlined.

    There was a language proposal to add special syntax for emitting a value from a code block, but it was rejected.

    0 讨论(0)
提交回复
热议问题