Scala Nothing data type

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. 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?

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. As we know, nothing is a lower type, but it's hard for me to find a connection between String and Nothing, as I said in the above example.

Thanks and Regards, Mohamed

+5
source share
2 answers

Now since Scala does not support multiple inheritance

Scala supports multiple inheritance using a mix. This is currently not commutative, i.e. Type A with B not identical to B with A (this will happen with Dotty), but it is still a form of multiple inheritance and is indeed one of Scala's strengths as it solves diamond problems through its linearization rules.

By the way, Null is another lower type inherited from Java (which can also be called the lower type Nothing , because you can throw an exception at runtime in any possible place).

I think you need to distinguish between class inheritance and type restrictions. There is no contradiction in defining Nothing as a lower type, although it is not โ€œexplicitlyโ€ inherited from any type that you need, such as List . This is more like an opportunity, the ability to throw an exception.

if we can assign List [Nothing] (Nil) to List [String], since List is covariant in Scala, as possible, because there is no direct correlation between Nothing and String type

Yes, the idea of โ€‹โ€‹the lower type is that Nothing also (among many others) a subtype of String . So you can write

 def foo: String = throw new Exception("No") 

This only works because Nothing (the type of throwing an exception) is more specific than the declared return type of String .

+3
source

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 .

+2
source

All Articles