Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Scala changed relative precedence of relational vs equality operators (compared to Java)?

Tags:

scala

In Java, < has higher priority than ==. In Scala it's vice versa. I wonder why Scala people chose that way? Other binary operator precedences align with Java (exept bitwise ops, but it's understandable why they didn't give special precedences for those).

UPDATE: It was actually a mistake in the language spec, '<' has actually higher priority than '==' in Scala.

like image 834
Aivar Avatar asked Aug 11 '11 07:08

Aivar


1 Answers

It's not inversed in Scala. Try this:

val what = 5 == 8 < 4

I get a compile-time warning: comparing values of types Boolean and Int using `==' will always yield false; so obviously the compiler has translated this to 5 == (8 < 4), just like in Java.

You can try this, too:

class Foo {
  def ===(o: Foo) = { println("==="); this }
  def <<<(o: Foo) = { println("<<<"); this }
  def >>>(o: Foo) = { println(">>>"); this }
}

def foo = new Foo

Then calling foo === foo <<< foo >>> foo prints this:

<<<
>>>
===

Which means it was parsed as (foo === ((foo <<< foo) >>> foo))

Can you provide an example where the precedence is reversed?

like image 199
Jean-Philippe Pellet Avatar answered Oct 15 '22 04:10

Jean-Philippe Pellet