Why does this not work?
val f = (args: Int*) => args.sum
error: ')' expected but identifier found.
val f = (args: Int*) => args.sum
^
This however works perfectly fine
def sum(args: Int*) = args.sum
val f = sum _
so does this
val f: (Int*) => Int = args => args.sum
btw. I'm using scala 2.9.1
I'm not an expert in specification reading, but it looks like the varargs Syntax is not supported for anonymous function.
Compare the syntax for Function Declaration vs Anonymous Functions in the Language Spec
ParamType ::= Type
| ‘=>’ Type
| Type ‘*’
Binding ::= (id | ‘_’) [‘:’ Type]
I have no idea what the reasons for that though. There seem to be reasons but none that can easily be explained. Martin Odersky commented on a request to add varargs for anonymous functions: "This looks tempting at first, but it would drag in a lot of complexity (you just need to believe me on that one)."
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With