2

Having:

case class A(x: Int) {
  def >>= (y: A) = A(x * y.x)
  def >> (y: A) = A(x + y.x)
}

Since >>= and >> both start with >, they should have the same precedence and left associativity.

However:

A(5) >>= A(3) >> A(2)

returns 25.

But should return 17, because:

A(5) >>= A(3) >> A(2) == (A(5) >>= A(3)) >> A(2) == 17

It looks like Scala compiler puts parantheses in the wrong place.

Bug?

ZhekaKozlov
  • 36,558
  • 20
  • 126
  • 155

1 Answers1

7

As this comment explains: operators which end with a = but don't start with one are considered assignment operators. They get the lowest precedence. This rule is needed for operators such as += and *=.

Community
  • 1
  • 1
Jasper-M
  • 14,966
  • 2
  • 26
  • 37
  • But these operators have nothing to do with assignment! – ZhekaKozlov Dec 08 '14 at 13:27
  • But they look like assignments. With mutable datastructures, for instance: `val list = ListBuffer.empty[Int]; list += 3` But I agree that Scala's precedence policy is not optimal :) – Jasper-M Dec 08 '14 at 13:36