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.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With