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

0

1 Answer 1

7

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

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.