Here are two REPL sessions (inspired by this question, although my question is different):
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
And:
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)
...
The only difference is that the first is Scala 2.9.2 and the second is 2.10.0.
Can someone point to the changes in 2.10 that lead to this new behavior?
I know that casting to Nothing is a silly thing to do, and that the answer might be “this is all undefined behavior so just stop doing that”, but it looks like the kind of thing that could potentially have implications for upgraders, and I don’t remember running into any discussions of changes that would explain this.
Since Scala treats
nulldifferently from theNonecase on an option, even anullvalue ofNothingis problematic–there should be exactly zero instances ofNothing, not one instance that may or may not break depending on how you use it.Thus, I cannot see how the old behavior is anything but a bug. It should be mentined in the release notes that it was fixed, but relying on
.asInstanceOf[Nothing]to do anything save throw an exception is sufficiently contrary to type-sanity that I don’t think anything more is needed. (In fact, I don’t even think the release note is needed.)