Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I cannot understand what is `Null` in Dart

I cannot understand what is Null in Dart.

In C# language null is a literal. The null is the default value of reference-type variable.

The null in C# has no type. It is not possible to access members of null through the dot ..

Example:

String s = null; // the default value of reference-type variable
null.GetType(); // Operator '.'  cannot be applied to operand of the type <null>

In Dart the null has a type Null. But instance of Null type in Dart is not a bottom type.

How this works in Dart according to the type inheritance?

// How the `null` value with type of `Null`
// can be assigned to a `String` type?
String s = null;

This code produce strange warning in Dart Editor.

"Tests for null should be done with '== null'".
print(s is NUll);

But I don't want "tests for null". I want test for Null. This is not the same.

Why this strange warning in Dart Editor?

I want test how to work Dart type system but the Editor get me this message.

I not understand it because I cannot understand how the Null type at the same time:

  1. can be subtype of all types
  2. can be not a subtype of all types

I welcome the answers from developers.

Is there any magic which is contrary to the object-oriented programming (that not documented in Dart language specification) about how the incompatible types can be assumed as the compatible types?

P.S.

The Null type in Dart is complete class and thus it is a regular type.

I not found official documentation which says the opposite (that Null type is not a regular type but it is a bottom type).

Here's proof of my words.

class Null {
  factory Null._uninstantiable() {
    throw new UnsupportedError('class Null cannot be instantiated');
  }

  /** Returns the string `"null"`. */
  String toString() => "null";
}

You can see that Null is not a bottom type.

Here you can see that null is not a bottom type but a Null type.

print(null.runtimeType == Null);
true

P.S.

I do not understand why some people do not understand the problems and lead contradictory examples.

The static type of null is bottom.

Not right. Type of null is Null type but not a bottom type.

I have already given proof that

  • null.runtimeType == Null but not a bottom type
  • Class Null is not bottom type (see source code in SDK).

P.S.

Why all of these answers with not correspond to reality (runtime) conclusions?

Or may be opponents are not developrers?

Or may be Dart runtime not follows language specification and opponents believe (or not know?) that this rule is not respected?

I think that null must have type of _Null that not declared in source code but created internally in virtual machine.

In this case I cannot say that _Null is not a bottom type because this hidden in virtual machine.

Who right? Me or you?

like image 743
mezoni Avatar asked Jan 11 '23 13:01

mezoni


1 Answers

From the spec

Null

The reserved word null denotes the null object.

nullLiteral: null

;

The null object is the sole instance of the built-in class Null. Attempting to instantiate Null causes a runtime error. It is a compile-time error for a class to attempt to extend or implement Null. Invoking a method on null yields a NoSuchMethodError unless the method is explicitly implemented by class Null.

The static type of null is bottom.

The decision to use bottom instead of Null allows null to be be assigned everywhere without complaint by the static checker.

bottom is poorly documented and I cannot find much reference to it. However, when the spec discusses void it is mentioned that :

bottom is a subtype of all types

So, to answer your questions, it would appear that there is nothing magical about the Null type but there is magic going on with the null literal; it has a type of bottom, which means it is a subtype of all types.

Since null is the only instance of Null, it is more correct to check if something == null than is Null, which I assume is why you get that warning from the editor.

like image 118
Pixel Elephant Avatar answered Jan 17 '23 15:01

Pixel Elephant