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:
- can be subtype of all types
- 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 abottom
type- Class
Null
is notbottom
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?
Null
is a subtype ofObject
and as Dart is a dynamic language assigningnull
(an instance ofNull
) to a variable of typeString
or any other type is valid. The static type checking must have a special rule to not produce an error or warning for assigningnull
to a variable of another type.Checking for
s is Null
results totrue
but the Dart style guide seems to mandates == null
for a reason unknown to me (maybe it's faster).