tl; dr summary : Nothing is a subtype of each type, because the specification says so. It is impossible to explain from the language. Each language (or at least almost every language) has some things in the kernel itself that cannot be explained from within the language, for example. java.lang.Object does not have a superclass, although each class has a superclass, because even if we do not write an extends clause, the class will implicitly receive a superclass. Or the bootstrap paradox in Ruby, Object is an instance of Class , but Class is a subclass of Object , and thus Object is an indirect instance of itself (and even more direct: Class is an instance of Class ).
I know Scala. Nothing is the bottom type. When I see the API, it spreads from "Any", which is the top of the hierarchy.
Now, since Scala does not support multiple inheritance, how can we say that it is a lower type.
There are two possible answers to this question.
A simple and short answer: because the specification says so . The specification says that Nothing is a subtype of all types, so Nothing is a subtype of all types. How? We do not care. The spectrum says so, so what is it. Let the compiler developers worry about how to present this fact in their compiler. You don't care how Any can have a superclass? Don't you like how def is represented inside the compiler inside?
A slightly longer answer: Yes, it is true, Nothing inherited from Any and only from Any . But! Inheritance is not the same as subtyping. In Scala, inheritance and subtyping are closely related, but they are not the same thing. The fact that Nothing can inherit only one class does not mean that it cannot be a subtype of more than one type. A type is not the same as a class.
In fact, to be specific, the specification does not even say that Nothing is a subtype of all types. He only says that Nothing matches all types .
In other words, it does not directly inherit all classes or traits such as Seq, List, String, Int, etc. If so, then how can we say that this is the bottom of the whole type?
Again, we can say that because the specification says we can say that.
How can we say that def defines a method? Because the specification says so . How can we say that abc means the same as ab(c) , and a b_: c means the same as { val __some_unforgeable_id__ = a; c.b_:(__some_unforgeable_id__) } { val __some_unforgeable_id__ = a; c.b_:(__some_unforgeable_id__) } ? Because the specification says so . How can we say that "" is a string and '' is a character? Because the specification says so .
I meant that if we can assign List [Nothing] (Nil) List [String], because List is covariant in Scala, as possible, because there is no direct correlation between Nothing and String.
Yes, there is a direct correlation between the types Nothing and String . Nothing is a subtype of String , because Nothing is a subtype of all types, including String .
As we know, nothing is a lower type, but it's hard for me to find a relationship between String and Nothing, as I said in the above example.
The relationship between String and Nothing is that Nothing is a subtype of String . What for? Because the specification says so.
The compiler knows that Nothing is a subtype of String just as it knows 1 is an Int instance and has a + method, even if you look at the Scala source code, the standard library of the Int class is actually abstract, and all its methods have no implementation .
Someone wrote code somewhere inside the compiler that knows how to handle adding two numbers , although these numbers are actually represented as JVM primitives and do not even exist inside the Scala object system. Similarly, someone somewhere wrote code inside the compiler that knows that Nothing is a subtype of all types , although this fact is not represented (or even represented) in the Nothing source code .