ron
ron

Reputation: 9364

Scala dotless call syntax with curried function

Note: A detailed answer to the more general problem is in Stack Overflow question What are the precise rules for when you can omit parenthesis, dots, braces, = (functions), etc.?.

The following works:

scala> List(1,2,3) filter (_ > 1) reduceLeft(_ + _)
res65: Int = 5

And also the following:

scala> List(1,2,3).filter(_ > 1).foldLeft(0)(_ + _)
res67: Int = 5

But not this sytax:

scala> List(1,2,3) filter (_ > 1) foldLeft(0)(_ + _)
<console>:10: error: 0 of type Int(0) does not take parameters
       List(1,2,3) filter (_ > 1) foldLeft(0)(_ + _)
                                        ^

What is a suggested fix?

Upvotes: 1

Views: 959

Answers (2)

Steve
Steve

Reputation: 19540

This topic is well described in Stack Overflow question What are the precise rules for when you can omit parenthesis, dots, braces, = (functions), etc.?.

Curried functions seems to be little bit harder than methods with one parameter. To omit the dot, curried functions need to use parenthesis outside the infix call.

As Marimuthu Madasamy mentioned, this works (the object (List), the method (foldLeft) and its first parameter (0) are in parenthesis):

(List(1,2,3) filter (_ > 1) foldLeft 0) (_ + _)

Upvotes: 7

Marimuthu Madasamy
Marimuthu Madasamy

Reputation: 13531

This works:

(List(1,2,3) filter (_ > 1) foldLeft 0) (_ + _)

Upvotes: 4

Related Questions