Why does foldLeft not support operator syntax?

97 views
Skip to first unread message

boris....@gmail.com

unread,
Mar 31, 2016, 6:18:54 AM3/31/16
to scala-debate
Scala supports operator syntax for methods of arity 1, so you can write
List(1,2,3) reduce (_+_)
while
List(1,2,3) foldLeft (0)(_+_)
fails with "error: Int(0) does not take parameters".

According to the style guide, you should use operator syntax for infix operators. However, the problem with dot-notation as explained in the style guide also applies to foldLeft:
List("a","b").foldLeft("") (_+_).toUpperCase

Also, why does foldLeft have curried parameters? This way, we can't write
List(1,2,3) foldLeft (0, _+_)

Kevin Wright

unread,
Mar 31, 2016, 6:29:41 AM3/31/16
to boris....@gmail.com, scala-debate
It’s all about type inference.

If I have method[T](a: T, b: T) and call it with two values a and b of different types then it’ll infer T to be the least upper bound of those two types.

If I have method[T](a: T)(b: T) and call it with two values a and b of different types then it’ll infer T to be the type of a, and will refuse to compile if the type of b doesn’t conform.

By having two parameter blocks, you can steer the compiler towards the type that you’re expecting it to return, and it can warn you if that won’t happen (instead of, e.g. inferring something useless like Any)



--
You received this message because you are subscribed to the Google Groups "scala-debate" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-debate...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Simon Schäfer

unread,
Mar 31, 2016, 6:31:45 AM3/31/16
to scala-...@googlegroups.com


On 03/31/2016 12:18 PM, boris....@gmail.com wrote:
Scala supports operator syntax for methods of arity 1, so you can write
List(1,2,3) reduce (_+_)
while
List(1,2,3) foldLeft (0)(_+_)
fails with "error: Int(0) does not take parameters".

According to the style guide, you should use operator syntax for infix operators. However, the problem with dot-notation as explained in the style guide also applies to foldLeft:
List("a","b").foldLeft("") (_+_).toUpperCase
Google search for "scala foldleft infix" reveals http://stackoverflow.com/questions/12904978 which should answer your question.


Also, why does foldLeft have curried parameters? This way, we can't write
List(1,2,3) foldLeft (0, _+_)
Google search for "scala multiple parameter lists" reveals http://stackoverflow.com/questions/4684185 which once again should answer your question.

boris....@gmail.com

unread,
Mar 31, 2016, 6:48:24 AM3/31/16
to scala-debate
Am Donnerstag, 31. März 2016 12:31:45 UTC+2 schrieb Simon Schäfer:

Ich habe nicht gefragt, warum ich ein . schreiben muß, sondern warum dieses Design gewählt wurde.

boris....@gmail.com

unread,
Mar 31, 2016, 6:50:24 AM3/31/16
to scala-debate, boris....@gmail.com
I see. However, the problem could be mitigated with explicit type annotation if the compiler can't infer a reasonable type.
Reply all
Reply to author
Forward
0 new messages