Pergunta

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

What is correct? Why?

Foi útil?

Solução

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.

Outras dicas

That is probably a bug in the analyzer. final and var are mutual exclusive.

One of the following is allowed

  • final identifier
  • final type identifier
  • const identifier
  • const type identifier
  • var identifier
  • type identifier

Dart Programming Language Specification (1.2) - Variables

finalConstVarOrType:
   final type?
   | const type?
   | varOrType
   ;
varOrType:
   var
   | type
   ;

EDIT

My DartEditor (Dart VM version: 1.3.0-dev.3.2 (Mon Mar 10 10:15:05 2014) on "linux_x64") shows an error for final var xxx (Members can not be declared to be both 'final' and 'var'.)

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top