-2

Right example:

"Hello".foldLeft(1L)((a, b) => a * b)

REPL prints errors:

"Hello" foldLeft(1L)((a, b) => a * b)

How is it explained? Is there some rule? I red that it is good to skip dots in Scala, but some examples don't work.

error: Long(1L) does not take parameters "Hello" foldLeft(1L)((a, b) => a * b)

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
TheSN
  • 125
  • 3

2 Answers2

1

You can use "infix notation" with methods of Arity-1 (single parameter). The convention is to use these for purely-functional methods. With curried functions (multiple parameter lists) like foldLeft, you can only use infix notation against the first parameter list (which would be confusing and you probably don't want to do this).

Related:

What are the precise rules for when you can omit parenthesis, dots, braces, = (functions), etc.?

The documentation:

http://docs.scala-lang.org/style/method-invocation

With curried functions:

Curried method call by infix notation

Community
  • 1
  • 1
nairbv
  • 4,045
  • 1
  • 24
  • 26
1

foldLeft is a curried function, and curried functions are non-intuitive to use with infix (dotless) notation. You need to wrap the infix call in parenthesis, like this:

scala> ("Hello" foldLeft 1L)((a, b) => a * b)
res0: Long = 9415087488

In this case, I would advise against this kind of usage.

For some general rules, you can look at the style guide.

The_Tourist
  • 2,048
  • 17
  • 21