The scala type Nothing
represents (as I understand it) the bottom of the type hierarchy, also denoted by the symbol ⊥. That is, Nothing
is a sub-type of any given type. The requirement for a Nothing
type is explained well by James Iry for those of us without a theoretical background in type theory!
So my question is, if Nothing
is a subtype of every type, why can I not call any type's methods on Nothing
? Obviously, I cannot instantiate Nothing but why doesn't the following compile?
var n: Nothing = _
def main(args: Array[String]) {
println(n.length) //compile error: value length is not a member of Nothing
}
Surely as Nothing
is a subtype of String
this should be OK? Note that the following compiles just fine!
var n: Nothing = _
def foo(s: String) : Int = s.length
def main(args: Array[String]) {
println(foo(n))
}
as does:
def main(args: Array[String]) {
println(n.asInstanceOf[String].length)
}