Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've digged into <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf" rel="nofollow">The Scala Language Specification</a> for Scala 2.9, and found out that part 6.23 describes how anonymous function have to be defined: </p> <pre><code>Expr ::= (Bindings | [‘implicit’] id | ‘_’) ‘=&gt;’ Expr ResultExpr ::= (Bindings | ([‘implicit’] id | ‘_’) ‘:’ CompoundType) ‘=&gt;’ Block Bindings ::= ‘(’ Binding {‘,’ Binding} ‘)’ Binding ::= (id | ‘_’) [‘:’ Type] </code></pre> <p>As you can see <code>Bindings</code> requires to be placed inside two surrounding parenthesis. So if your anonymous function defines the type of the parameter, it <strong>has</strong> to be defined in this way:</p> <pre><code>(c:Char) =&gt; 1 </code></pre> <p>And the call to map should look like that:</p> <pre><code>s.toList map((c:Char) =&gt; 1) </code></pre> <p>Also in the same part of reference documentation you can find:</p> <blockquote> <p>If an anonymous function (x: T) => e with a single typed parameter appears as the result expression of a block, it can be abbreviated to x: T => e.</p> </blockquote> <p>So it says that if anonymous function is defied as last expression in a code block, and has exacly one parameter, you can use abbreviated syntax to define anonymous function - without parenthesis. So, in your case, you can write <code>c:Char =&gt; 1</code> but only when you place it inside a code block <code>{c:Char =&gt; 1}</code>. And you can call map function this way:</p> <pre><code>s.toList map({c:Char =&gt; 1}) </code></pre> <p>or with abbreviated syntax without parenthesis:</p> <pre><code>s.toList map {c:Char =&gt; 1} </code></pre> <p>On the other hand, when we look back at the anonymous function specification:</p> <pre><code>Expr ::= (Bindings | [‘implicit’] id | ‘_’) ‘=&gt;’ Expr </code></pre> <p>We can see that we can define your anonymous function, if we don't want to specify argument type, without parameter binding in those two ways:</p> <pre><code>s.toList map (c =&gt; 1) // or s.toList map (_ =&gt; 1) </code></pre> <hr> <p>Also, this is summarized in changelog at the end of the documentation (Changes in Version 2.1.7 (19-Jul-2006)):</p> <p>Closure Syntax</p> <p>The syntax of closures has been slightly restricted (§6.23). The form</p> <pre><code>x: T =&gt; E </code></pre> <p>is valid only when enclosed in braces, i.e. <code>{ x: T =&gt; E }</code>. The following is illegal, because it might be read as the value x typed with the type T => E:</p> <pre><code>val f = x: T =&gt; E </code></pre> <p>Legal alternatives are:</p> <pre><code>val f = { x: T =&gt; E } val f = (x: T) =&gt; E </code></pre>
    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.
    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