What does the static Dart type mean and why is it different from the runtime type?

There are two types of types in Dart.

  • Type of execution
  • Static type

Here's the proof in the Dart Language Specification :

static type from null is lower.

  • Execution Type null - null
  • Static type null - bottom

This means that objects in Dart can have two types of types.

One real type, called static and one virtual , which is called runtime .

That is, the null runtime type is not bottom , but a regular null class.

 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, a value with this regular runtime type null can be assigned to any other type, because the real (static) type null is the bottom type.

What is the name of this method in Dart?

Type substitution or something else?

PS

This question is about static value types , but not about static variable types declared by type annotations.

This is because null not a variable, but value with a static type from bottom .

PS

A very interesting 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' 

This is absolutely correct. But at the same time it works.

Unusual thing with type replacement (static and runtime).

+2
source share
2 answers

Execution Type Dart value - its class. Static Dart Expression Type - this is what its inference is of a static type, and it belongs to the world of static types. This world is more than just classes declared in the program. The bottom type, dynamic type, and function type int-> int are all examples of static types that do not correspond to the class.

Or, in other words: values ​​have classes, expressions have types (as in many other languages). There is no "static type of values" because static types exist at compile time, and values ​​exist only at run time [1].

The static type inference algorithm is specified in the Dart language specification. This is what is needed, and all that is required of it is that it is somehow compatible with the execution of the program at run time.

A static type system is a software analysis that tries to detect probable programming errors, nothing more and nothing more. If you have a warning of a static type, it is considered probable, but not sure that you have an error. The type inference system should give some false warnings and not detect some actual errors, and is also simple enough to describe, understand, and implement.

Choosing a “bottom” as a type of “null” is just a way to get a static type system to match an “assignable” relationship between types, which is checking runtime assignments, without having to explicitly specify a “Null” value, everywhere. This is just an algorithm that gives a useful result.

The static type does not exist at run time. For example, a virtual machine does not use a static type system implementation at all.

/ l

[1] Well, except for compile-time constant expressions.

+4
source

When you declare type annotations and type records, these are static types. Since type annotations are not used at run time, you can assign a value of type Foo variable of type Bar . Even if the static type is Foo , the execution type is Bar .

Perhaps wrong. I have not yet begun to examine these issues in more detail.

0
source

All Articles