Why brackets around int when calling scala method

I read a Scala tutorial and explains that all statements are actually method calls. So 1 * 2 really:

 scala> (1).*(2) res1: Int = 2 

To find out what will happen, I ran:

 scala> 1.*(2) warning: there were 1 deprecation warning(s); re-run with -deprecation for details res2: Double = 2.0 

So, I run it again with the deprecation flag, and I get:

 scala> 1.*(2) <console>:1: 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. 1.*(2) 

Can someone please explain this warning to me, and also explain to me what the parentheses around 1 for scala> (1).*(2) .

+7
scala
source share
1 answer

When you say 1.*(2) , it is ambiguous as to whether you have:

(1).*(2) , which leads to Int

or

(1.)*(2) , which leads to double, since 1. is a valid syntax meaning Double 1.0

Scala currently considers it to be the second, but since the correct behavior is not obvious, it will change from Scala 2.11 onwards to consider it as the first. Scala warns you that its behavior will change.

+21
source share

All Articles