Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When you write an expression <code>c:Char =&gt; 1</code>, Scala compiler treats it as <code>c:(Char =&gt; 1)</code>, that is, a function variable <code>c</code> with type <code>Char =&gt; 1</code>. However, <code>1</code> is not a valid type (or type variable), so Scala compiler complains. So you need to write <code>s.toList map ((c:Char) =&gt; 1)</code> to help the compiler out of the type association.</p> <p><code>{c:Char =&gt; 1}</code> creates a function literal, that is, a function that accepts a char and returns 1. So</p> <pre><code>s.toList map {c:Char =&gt; 1} </code></pre> <p>is the same as</p> <pre><code>s.toList map({c:Char =&gt; 1}) // or val fn = {c: Char =&gt; 1} s.toList map fn </code></pre> <p><del>However, it seems that Scala doesn't have special specification for the <code>{c:Char =&gt; blabla}</code> form for creating function literals. </del></p> <p><del>I think it is because that there is <code>{some_var: SomeType =&gt; *Class Definition* }</code> form in class definition to reference <code>this</code> with <code>some_var</code> and add lower bound of the type of <code>this</code>. This causes the Scala lexer to treat a single typed parameter after <code>{</code> to be formal parameter and treat the statements after <code>=&gt;</code> to be actual body. Note that you <strong><em>CANNOT</em></strong> create a function with 2 parameters like this <code>{c: Char, d:Char =&gt; 1}</code>.</del></p> <p><strong>UPDATE</strong>: As commented by <em>som-snytt</em>, it is result expression that causes the brace version to be evaluated to a function result.</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. 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.
 

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