rajasaur
rajasaur

Reputation: 5460

Adding spaces to the + operator in Scala gives different results?

Scala newbie here

Trying

(1).+(2) returns a Int value of 3, so far so good
but
1.+(2) returns a Double value of 3.0.

But if you do
1 . +(2) it returns a Int value of 3.
Note: The only difference between this and the above is the space after the "1"

Does Spaces matter in Scala? Im more curious as to how 1.+(2) returned a Double as it looks like it parsed 1. as a Double and then added "2" to it.

Upvotes: 5

Views: 577

Answers (1)

mpilquist
mpilquist

Reputation: 3855

1.+(2) is calling the + method on the Double "1.". This is a carry-over from Java syntax, where "1." is equivalent to 1.0.

Upvotes: 11

Related Questions