5

In other words, is there a good reason why this shouldn't compile?

def f(xs: List[Int]) = xs.foldLeft(0) _  // OK
def f(xs: List[Int]) = (xs :\ 0) _       // OK
def f(xs: List[Int]) = (0 /: xs) _

<console>:15: error: missing arguments for method /: in trait TraversableOnce;
follow this method with `_' if you want to treat it as a partially applied function

Here are some workarounds:

def f(xs: List[Int]) = xs./:(0) _
def f(xs: List[Int]): ((Int, Int) => Int) => Int = (0 /: xs)

but my question is mainly about the proper syntax in general.

2
  • 2
    +1, and I just found another confusing compiler error when omit a pair of parentheses in expression def f(xs: List[Int]): (Int, Int) => Int => Int = (xs :\ 0) found : (Int, Int) => Int => Int required: (Int, Int) => Int => Int
    – 4e6
    Oct 12, 2011 at 6:00
  • Opened issues.scala-lang.org/browse/SI-5073 Oct 14, 2011 at 4:07

2 Answers 2

2

I fixed this just now, but I can't check it in yet because it requires amending the specification.

scala> def f(xs: List[Int]) = (0 /: xs) _
f: (xs: List[Int])(Int, Int) => Int => Int

scala> f(1 to 10 toList)
res0: (Int, Int) => Int => Int = <function1>

scala> res0(_ + _)
res1: Int = 55

The problem is that the spec defines "e1 op e2" if op is right-associative to be { val x=e1; e2.op(x ) } for reasons which are not apparent to me, since the simpler e2.op(e1) solves this issue among others, like https://issues.scala-lang.org/browse/SI-1980. I will make inquiries.

1
  • Nope, don't hold your breath. Impact would be too large.
    – psp
    Oct 14, 2011 at 5:22
2

Looks like a compiler bug. I've tested this expressions on different scala versions and what I've got for:

def f(xs: List[Int]) = (0 /: xs) _

It behaves the same for 2.9.1.final and 2.8.2.final but for 2.7.7.final it fires different error message (Iterable vs. TraversableOnes), but I think it's because of collections library redesign in older versions.

def f(xs: List[Int]) = (0 /: xs) _
<console>:4: error: missing arguments for method /: in trait Iterable;
follow this method with `_' if you want to treat it as a partially applied function

Expression, that I mentioned in comment behaves different for different scala versions.

def f(xs: List[Int]): (Int, Int) => Int => Int = (0 /: xs)

scala 2.9.1.final:

 found   : (Int, Int) => Int => Int
 required: (Int, Int) => Int => Int

Really confusing compiler message, definitely a bug.

scala 2.8.2.final:

 found   : => ((Int, Int) => Int) => Int
 required: (Int, Int) => (Int) => Int

Weird => in the beginning, in comparison to 2.7.7.final result looks like a regression.

scala 2.7.7.final:

 found   : ((Int, Int) => Int) => Int
 required: (Int, Int) => (Int) => Int

found is seemingly right but code is still not working.

I searched on scala bugtracker for similar issues but could not find anything suitable. Think it's enough to create a ticket (or two? looks like this two errors are not related).

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.