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

3 篇文章 0 订阅
2 篇文章 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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值