Allow constructor initializers to access fields initialized from previous initializers
TL;DR support:
class Foo {
Foo()
: a = DateTime.now(),
b = 'Hello $a'; // < initialize b from a
final DateTime a;
final String b;
}
The idea is to have the order of initializers matter.
Would be nice, but needs to be specified as something different than "accessing the getter".
Both because there might be a getter returning a different value, and because you cannot refer to this yet. Something like:
If the initializer list contains an initialization of an instance variable named id, and the constructor does not declare a parameter named id, then the initializer list scope contains a final variable named id. It is a compile time error to refer to that variable in the initializer list prior to or inside the initializer for id. After that initializer has been executed, initializing the instance variable to a value v, the initializer list variable id is bound to the value v.
Basically the same kind of variable as introduced by this.id.
TL;DR support:
class Foo { Foo() : a = DateTime.now(), b = 'Hello $a'; // < initialize b from a final DateTime a; final String b; }The idea is to have the order of initializers matter.
You can use redirecting constructors like this if you really need the previously initiallized value.
class Foo {
Foo():this._(DateTime.now());
Foo._(Datetime d): a =d, b='Hello $d';
final DateTime a;
final String b;
}
That's not the same thing, and super verbose :)
It seems like this will incentivize people to declare fields just to use as temporary variables during initialization.