Why does the Scala 2.11.0-M3 compiler give me error: identifier expected but integer literal found. when the round brackets () are use
I've digged into The Scala Language Specification for Scala 2.9, and found out that part 6.23 describes how anonymous function have to be defined:
Expr ::= (Bindings | [‘implicit’] id | ‘_’) ‘=>’ Expr
ResultExpr ::= (Bindings | ([‘implicit’] id | ‘_’) ‘:’ CompoundType) ‘=>’ Block
Bindings ::= ‘(’ Binding {‘,’ Binding} ‘)’
Binding ::= (id | ‘_’) [‘:’ Type]
As you can see Bindings requires to be placed inside two surrounding parenthesis. So if your anonymous function defines the type of the parameter, it has to be defined in this way:
(c:Char) => 1
And the call to map should look like that:
s.toList map((c:Char) => 1)
Also in the same part of reference documentation you can find:
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.
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 c:Char => 1 but only when you place it inside a code block {c:Char => 1}. And you can call map function this way:
s.toList map({c:Char => 1})
or with abbreviated syntax without parenthesis:
s.toList map {c:Char => 1}
On the other hand, when we look back at the anonymous function specification:
Expr ::= (Bindings | [‘implicit’] id | ‘_’) ‘=>’ Expr
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:
s.toList map (c => 1)
// or
s.toList map (_ => 1)
Also, this is summarized in changelog at the end of the documentation (Changes in Version 2.1.7 (19-Jul-2006)):
Closure Syntax
The syntax of closures has been slightly restricted (§6.23). The form
x: T => E
is valid only when enclosed in braces, i.e.
{ x: T => E }. The following is illegal, because it might be read as the value x typed with the type T => E:
val f = x: T => E
Legal alternatives are:
val f = { x: T => E }
val f = (x: T) => E