In Dart exists two kind of types.
- Runtime type
- Static type
Here is proof in Dart language specification:
The static type of null is bottom.
- Runtime type of
null
isNull
- Static type of
null
isbottom
This mean that objects in Dart can have two kind of types.
One real type that called static
and one virtual
type that called runtime
.
That is, runtime type of null
is not a bottom
but a regular class Null
.
class Null {
factory Null._uninstantiable() {
throw new UnsupportedError('class Null cannot be instantiated');
}
/** Returns the string `"null"`. */
String toString() => "null";
}
But at the same time value with this regular runtime type Null
can be assigned to any other type because real (static) type of the null
is a bottom
type.
How called this technique in Dart?
The type substitution or something different?
P.S.
This question about static types of values but not about static types of variables that declared with type annotations.
This is because the null
is not a variable but value
with static type
of bottom
.
P.S.
Very curious case (at least for me).
void main() {
Null _null;
String s = _null;
}
I get a warning:
A value of type 'Null' cannot be assigned to a variable of type 'String'
It is quite correct. But at the same time this works.
Сurious thing with type substitution (static and runtime).
When you declare a class and write type annotations this are static types. As type annotations are not used at runtime you can assign a value of type
Foo
to a variable of typeBar
. Even if the static type isFoo
the runtime type isBar
.Might be wrong. I haven't taken a closer look at those matters yet.