Shorthand arithmetic operators return different results in Scala - EG: 3 + 2 != 3.+(2) [duplicate]

StackOverflow https://stackoverflow.com/questions/11909421

Question

Possible Duplicate:
Scala operator oddity

I'm very new to Scala and I read that in this language everything is an Object, cool. Also, if a method has only 1 argument, then we can omit the '.' and the parentesis '( )', that's ok.

So, if we take the following Scala example: 3 + 2, '3' and '2' are two Int Objects and '+' is a method, sounds ok. Then 3 + 2 is just the shorthand for 3.+(2), this looks very weird but I still get it.

Now let's compare the following blocks on code on the REPL:

scala> 3 + 2
res0: Int = 5

scala> 3.+(2)
res1: Double = 5.0

What's going on here? Why does the explicit syntax return a Double while the shorthand returns an Int??

Était-ce utile?

La solution

3. is a Double. The lexer gets there first. Try (3).+(2).

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top