0

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??

Community
  • 1
  • 1
Marsellus Wallace
  • 17,991
  • 25
  • 90
  • 154

1 Answers1

7

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

Rex Kerr
  • 166,841
  • 26
  • 322
  • 407
  • 3
    yup, but note that if you type `3.+(2)` in to Scala 2.10 you get `warning: This lexical syntax is deprecated. From scala 2.11, a dot will only be considered part of a number if it is immediately followed by a digit.` – Seth Tisue Aug 10 '12 at 23:15