这里有两个REPL会议(受这个问题的启发,虽然我的问题不同):
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
Run Code Online (Sandbox Code Playgroud)
和:
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)
...
Run Code Online (Sandbox Code Playgroud)
唯一的区别是第一个是Scala 2.9.2,第二个是2.10.0.
有人能指出导致这种新行为的2.10中的变化吗?
我知道转换Nothing是一件很愚蠢的事情,答案可能是"这是所有未定义的行为所以只是停止这样做",但它看起来像是可能对升级者产生影响的事情,我不喜欢不记得遇到任何可以解释这种情况的变化的讨论.
由于Scala对选项的处理方式null不同None,即使null值Nothing是有问题的 - 应该确实存在零实例Nothing,而不是一个可能会或可能不会中断的实例,具体取决于您使用它的方式.
因此,我无法看到旧的行为是什么,而不是一个错误.应该在发行说明中说明它是固定的,但依赖于.asInstanceOf[Nothing]做任何事情除了抛出异常与类型理智完全相反,我认为不需要更多.(事实上,我甚至不认为需要发布说明.)
| 归档时间: |
|
| 查看次数: |
466 次 |
| 最近记录: |