language
language copied to clipboard
Make `augmented` an error in the body of a non-redirecting generative constructor?
The semantics of augmented()
in the body of an augmenting declaration of a non-redirecting generative constructor is complex, and it treats parameters in a way which is different from the treatment of parameters of other functions.
class A {
A(int i) {
print(i);
}
augment A(i) {
i = i + 1;
augmented(); // NB: just `augmented()`, not `augmented(i)`.
}
}
void main() {
A(0); // Prints '1'.
}
I'd recommend that we postpone the discussions about how to handle constructor body augmentations in this case, and simply specify that it is an error to specify a body in an augmenting declaration of this kind of constructor: The body can be provided by at most one declaration (introductory or augmenting) in the stack, and it is an error to provide more than one.
These tricky details probably arise because a non-redirecting generative constructor can have initializing formal parameters, and we can't allow the invocation of augmented()
to repeat the initialization of certain final instance variables, nor can we allow the omission of an invocation of augmented()
to allow those instance variables to remain uninitialized. (A super-parameter may seem similar to an initializing formal, but they are probably easier to handle.)
In the future, we can choose to support case where the constructor body is augmented in any way we want because it will be a non-breaking change.
@dart-lang/language-team, WDYT?