Why Scala changed relative precedence of relational vs equality operators (compared to Java)?
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?
精彩评论