How is Nothing a subtype of every other type in Scala

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?

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật