Being the subtype of every other type allows a hypothetical Nothing typed value to be passed to any function. However, although such a value can serve as receiver for toString() it can't for unary_! (among others).
object Foo {
def dead(q: Nothing): Unit = {
println(q);
q.toString();
((b: Boolean) => !b)(q);
!q; // value unary_! is not a member of Nothing
}
}
Is this a bug or a feature?
Note:
- This is the Scala version of an equivalent question I asked on Kotlin.
- Upcasting works:
!(q.asInstanceOf[Boolean])
In other words, there are no values of type
Nothing. So contrary to the statement in your question, you can't pass aNothingtyped value to any function (even hypothetically) because it doesn't exist, by definition. Neither can it be a receiver for any method because, again, it doesn't exist.So the bug, if there is one, is that the compiler does not warn you that you have created a function that can never be called.
In this case,
println(q)works becauseNothingis a subtype ofAny, andq.toStringworks because of an implicit conversion ofAnyReftoObjectwhich supportstoString. The inline function convertsqtoBooleanwhich is also OK, butObjectdoes not supportunary_!so!qfails to compile.