I am taking Martin Odersky’s coursera course on functional programming with scala, and for now I have learned two things that together don’t make sense:
- Scala doesn’t support multiple inheritance
Nothing
is a subtype of every other type
These two statements cannot live together, so how exactly is this done? and what exactly is the meaning of “subtype of every other type”
Edit 1
In the Scala API, Nothing
is defined as abstract final class Nothing extends Any
… so how can it extend other classes?
7
Subtyping and inheritance are two different things! Nothing
doesn’t extend everything, it’s a subtype, it only extends Any
.
The specification[§3.5.2] has a special case governing the subtyping-relationship of Nothing
:
§3.5.2 Conformance
- […]
- For every value type
T
,scala.Nothing <: T <:scala.Any
- For every type constructor
T
(with any number of type parameters)
scala.Nothing <: T <: scala.Any
- […]
Where <:
basically means “is a subtype of”.
As for how this is done: We don’t know, it’s compiler magic and an implementation detail.
Quite often a language does things you as a programmer can’t. As a counterpart to Nothing
: Everything in Scala inherits from Any
, everything except Any
. Why doesn’t Any
inherit from something? You can’t do that. Why can Scala do that? Well, because Scala set the rules, not you. Nothing
being a subtype of everything is just an other instance of this.
3
When he says that Scala doesn’t support multiple inheritance, then he refers to inheriting a method implementation multiple times. Of course, you can implement multiple interfaces/traits in a class, and they can even define the same method, but you do not get a conflict between the different implementations due to trait linearization.
In general, if you have a class C1
with a method f()
and a class C2
also with a method f()
, then multiple inheritance means you can somehow inherit both implementations of f()
. This can lead to various problems, which Scala resolves by only letting you inherit from a single class and in case of multiple traits by selecting one implementation based on the order of the traits.
As for Nothing
things are really simple, because nothing has no attributes or methods defined. So you cannot have any inheritance conflicts. But I assume that most of your surprise comes from a different understanding of multiple inheritance.
Once you understand that trait linearization effectively eliminates any ambiguity of the inheritance, and that we do not refer to inheriting from multiple traits as multiple inheritance due to that, then you should be fine.
As to how this is realized: the compiler is eventually responsible for this. See the Scala language specification section 3.5.2 conformance, which amongst other properties includes:
For every type constructor T (with any number of type parameters), scala.Nothing <: T <: scala.Any.
Or in other words, if you want to implement a compiler correctly, it has to handle Nothing
as a subtype of everything by specification. For obvious reasons, Nothing
is not defined to extend from all classes loaded into the system, but the relevance of defining Nothing
as subtype is limited to all places, where subtyping is relevant.
An important point here is that there exists no instance of type Nothing
, hence, its treatment is strictly limited to type-checking, which is all in the realm of the compiler.
2