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 aNothing
typed 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 becauseNothing
is a subtype ofAny
, andq.toString
works because of an implicit conversion ofAnyRef
toObject
which supportstoString
. The inline function convertsq
toBoolean
which is also OK, butObject
does not supportunary_!
so!q
fails to compile.