Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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:

  1. Scala doesn't support multiple inheritance
  2. 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?

share|improve this question
    
This page might help a bit: artima.com/pins1ed/scalas-hierarchy.html –  jhewlett Apr 23 '13 at 5:52
    
As far as I can see it is defined as "final trait Nothing extends Any" scala-lang.org/api/2.7.6/scala/Nothing.html –  Den Apr 23 '13 at 8:06
7  
You are confusing types and classes. Those two are very different things. Unfortunately, you are not the only one who is confused by that distinction, and really unfortunately, some of those who are confused happen to be the designers of popular languages like Java, C# and C++. It doesn't say that Nothing is a subclass of every other class. It says that it is a subtype of every other type. –  Jörg W Mittag Apr 23 '13 at 10:39
1  
@delnan: Java's interfaces are taken directly from Smalltalk's protocols. In Smalltalk, only protocols are types, classes aren't. In Java, both interfaces and classes are types. That's wrong. Classes aren't types, only interfaces are. The fact that all of these languages have things that are types and not classes is irrelevant. The problem is that in those languages classes are types, which is wrong. –  Jörg W Mittag Apr 23 '13 at 12:15
1  
@JörgWMittag That's a different statement, and extremely arguable (I tend to agree that it's harmful, but I would not attribute this to a misunderstanding of typing). No point in discussing it here. –  delnan Apr 23 '13 at 12:24

2 Answers 2

up vote 20 down vote accepted

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.

share|improve this answer
7  
BTW: this is exactly the same as null being assignable to a field of every type in Java. Why is that possible? Is null an instance of every class? No, it is possible because the compiler says so. Period. –  Jörg W Mittag Apr 23 '13 at 10:34
7  
If I could upvote this a hundred times, I would. Confusing types and classes is one of the worst things languages like Java have brought upon us. –  Jörg W Mittag Apr 23 '13 at 10:36

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.

share|improve this answer
2  
What I still don't understand is how this is done... See the edit to my question –  vainolo Apr 23 '13 at 7:19
1  
"the relevance of defining Nothing as subtype is limited to all places, where subtyping is relevant." What do you want to convey with that? X is relevant where X is relevant? –  phant0m Apr 23 '13 at 10:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.