Why `final var` is illegal in Dart?

Issue

The analyzer doesn’t say final var is illegal.
but dart2js says final var is illegal

What is correct? Why?

Solution

The keyword var means mutable variable with explicit dynamic type specifier.
The explicit type specifier means that this is not possible specify another type in declaration.

The keyword final means val or immutable variable with unspecified type, with implicit dynamic type.
The implicit type specifier means that this is possible specify other type in declaration.

More precisely variable that declared as val are the value and variable at once.

It are variable because has the runtime storage.

But it are also immutable value that can be retrieved from associated storage just once and can be used anywhere.

Now consider the following code:

final var foo;

This is the same as the following pseudo code:

immutable mutable dynamic foo;

Of course, this will not work.

Answered By – mezoni

Answer Checked By – David Marino (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *