关于scala浮点数省略小数点后0警告的讨论

在REPL中运行val s = 3.将会发生如下情况

scala> val s = 3.
warning: there were 1 deprecation warning(s); re-run with -deprecation for details
s: Double = 3.0

而运行val s = 3则不会发生警告

scala> val s = 3
s: Int = 3

我们退出scala并使用scala -deprecation重新进入

scala> val e = 3.
<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.
       val e = 3.
               ^
<console>:7: 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.
       val e = 3.
               ^
e: Double = 3.0

会警告将会在2.11被禁用

是因为scala是完全面向对象的语言,不同于java,scala可以使用点操作符来调用加号运算符

scala> (2).+(3)
res4: Int = 5

所以将会产生歧义,到底是浮点数调用加号运算,还是整数调用点运算

在使用(2.)+(3)后仍然可以得到结果,但是会产生警告

scala> (2.)+(3)
<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.
       (2.)+(3)
        ^
<console>:2: 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.
              (2.)+(3)
               ^
<console>:8: 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.
              (2.)+(3)
               ^
res2: Double = 5.0

或者也可以这样使用 (2.).+(3)

scala> (2.).+(3)
<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.
       (2.).+(3)
        ^
<console>:2: 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.
              (2.).+(3)
               ^
<console>:8: 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.
              (2.).+(3)
               ^
res1: Double = 5.0

但是不加括号会导致编译错误

scala> 2..+3
<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.
       2..+3
       ^
<console>:1: error: ';' expected but integer literal found.
       2..+3
           ^

scala> (2.).+3
<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.
       (2.).+3
        ^
<console>:1: error: ';' expected but integer literal found.
       (2.).+3

你可能感兴趣的:(shell,scala,极致通俗,java,bug,c++,scala,jvm)