Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When you write</p> <p>{ i: Int => 2 * i }</p> <p>the braces are a block, and the thing inside is the result expression of the block.</p> <p>The syntax is <a href="https://github.com/scala/scala-dist/blob/master/documentation/src/reference/SyntaxSummary.tex#L158" rel="nofollow noreferrer">the ResultExpr here</a>. Note that a type is allowed after the param name, as above.</p> <p>That's different from <a href="https://github.com/scala/scala-dist/blob/master/documentation/src/reference/SyntaxSummary.tex#L114" rel="nofollow noreferrer">an Expr</a> where it is not. </p> <p>Here's an example of the difference:</p> <pre><code>scala&gt; val is = List(1,2,3) is: List[Int] = List(1, 2, 3) </code></pre> <p>The first statement in block has a function literal, an Expr requiring parens if we specify the type. The last statement is the result expression of the block, and no parens are required:</p> <pre><code>scala&gt; is map { val f = (i: Int) =&gt; 2*i; i: Int =&gt; 2*f(i) } res0: List[Int] = List(4, 8, 12) </code></pre> <p>Sometimes you don't need to specify the type because it is inferred from the expected type, and with no type in the Expr, you can omit the parens:</p> <pre><code>scala&gt; is map { val f: Int=&gt;Int = i =&gt; 2*i; i: Int =&gt; 2*f(i) } </code></pre> <p>The <a href="https://github.com/scala/scala-dist/blob/master/documentation/src/reference/SyntaxSummary.tex#L207" rel="nofollow noreferrer">Bindings</a> in these productions (in the spec) is your ordinary list of params in parens, which maybe have types. With more than one param, you have to supply parens:</p> <pre><code>scala&gt; is reduce { val f = (i:Int,j:Int) =&gt; i+j; (i:Int,j:Int) =&gt; 2*f(i,j) } res2: Int = 18 </code></pre> <p>Sometimes you'll see a big block of code as an argument to a function. That's a big function literal as the result expression of a block, which is why you see the parameter sitting out in front, with no parens or braces:</p> <pre><code>scala&gt; is map { i =&gt; val j = 2 * i; /* lots of LOC */ ; j } res7: List[Int] = List(2, 4, 6) </code></pre> <p>That is different from the following code, which is block of many lines of code with the function literal at the very end. The function just returns <code>j</code>, or 2:</p> <pre><code>scala&gt; is map { val j = 2; /* lots of LOC */ ; _ =&gt; j } res8: List[Int] = List(2, 2, 2) </code></pre> <p>So we know that you can't write the following:</p> <pre><code>scala&gt; is map (is: Int =&gt; 2) &lt;console&gt;:1: error: identifier expected but integer literal found. is map (is: Int =&gt; 2) ^ </code></pre> <p>What sort of identifier would be meaningful in this context? How about:</p> <pre><code>scala&gt; is map (is: Int =&gt; Int) </code></pre> <p>which yields the delightful result (spoiler alert):</p> <pre><code>java.lang.IndexOutOfBoundsException: 3 </code></pre> <p>This works:</p> <pre><code>scala&gt; val js = List(0,1,2) js: List[Int] = List(0, 1, 2) scala&gt; js map (js: Int =&gt; Int) res0: List[Int] = List(0, 1, 2) </code></pre> <p>The <code>js</code> in parens is just a value, obviously, not a param, and the type is a type ascription. The value can be a post-fix expression, so this works (or rather, compiles with a feature warning about the post-fix operator syntax):</p> <pre><code>scala&gt; js map (js init: Int =&gt; Int) warning: there were 1 feature warning(s); re-run with -feature for details java.lang.IndexOutOfBoundsException: 2 </code></pre> <p>More explanation at this answer:</p> <p><a href="https://stackoverflow.com/a/13873899/1296806">https://stackoverflow.com/a/13873899/1296806</a></p> <p>There is a category of confusion caused by thinking that parens and curly braces are somehow exchangeable. But I found it clarifying to see braces as <code>BlockExprs</code>, which is what they are. Then it's easier to remember, for instance, that when you use braces in a function application, there is no magic: you've simply supplied a <a href="https://github.com/scala/scala-dist/blob/master/documentation/src/reference/SyntaxSummary.tex#L149" rel="nofollow noreferrer">block</a>. </p> <p>A block is a bunch of side-effecting statements followed by a result statement. That's obvious for functional programmers, perhaps. But it clarifies that the last thing in your braces is a <code>ResultExpr</code>.</p> <p>(Footnote: braces for class bodies are different, of course.)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload