【发布时间】:2012-12-28 14:32:28
【问题描述】:
这里有两个 REPL 会话(受 this question 启发,虽然我的问题不同):
Welcome to Scala version 2.9.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0).
Type in expressions to have them evaluated.
Type :help for more information.
scala> def ignore(it: String) = 42
ignore: (it: String)Int
scala> ignore(null.asInstanceOf[Nothing])
res0: Int = 42
还有:
Welcome to Scala version 2.10.0 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0).
Type in expressions to have them evaluated.
Type :help for more information.
scala> def ignore(it: String) = 42
ignore: (it: String)Int
scala> ignore(null.asInstanceOf[Nothing])
java.lang.NullPointerException
at .<init>(<console>:9)
at .<clinit>(<console>)
at .<init>(<console>:7)
at .<clinit>(<console>)
at $print(<console>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
...
唯一的区别是第一个是 Scala 2.9.2,第二个是 2.10.0。
有人能指出导致这种新行为的 2.10 中的变化吗?
我知道强制转换为 Nothing 是一件很愚蠢的事情,而答案可能是“这都是未定义的行为,所以请停止这样做”,但它看起来可能会产生影响对于升级者,我不记得有任何讨论可以解释这一点。
【问题讨论】:
-
由于 2.10 尚未稳定,因此将其列为错误可能是个好主意。如果不是,则应记录在案。如果是,应该在2.10final之前修复
标签: scala scala-2.10