Multiline function literal as arguments in Scala

后端 未结 2 706
无人及你
无人及你 2020-11-29 07:53

I always wondered why sometimes with function literals we can ignore the curly brace even for multiple statements. To illustrate this, the syntax for a multiline function li

2条回答
  •  情话喂你
    2020-11-29 08:06

    There are just a couple of simple syntax rules. The appendix of the spec is worth perusing.

    A function literal or anonymous function (6.23) will look like x => Expr or x => Block depending on whether the context is an Expr or a ResultExpr, respectively.

    A function application (6.6) will look like f(Expr, Expr) or f BlockExpr, i.e., f{ Block }. That is, a BlockExpr is just a sequence of block statements inside {...}.

    When you call f(g), then g is an Expr, so as a function literal, x => Expr. The Expr can be a BlockExpr, x => { ... }.

    When you call f{ Block }, then f { x => ... } has the function literal in ResultExpr of a Block (which is just a sequence of statements, no braces required).

    Here, it's obvious that the anon func is at the bottom of a block:

    scala> def m(x: Int=>Int) = x(5)
    m: (x: Int => Int)Int
    
    scala> m {
         | val y = 7
         | x => // no brace
         | x+y+1
         | }
    res0: Int = 13
    

提交回复
热议问题