dart-lang / language

Design of the Dart language
Other
2.61k stars 200 forks source link

Is this augmenting variable declaration actually allowed? #3958

Open eernstg opened 4 days ago

eernstg commented 4 days ago

The augmentation specification seems to allow the following configuration:

// Original.
int x = 1;

// First level of augmenting declarations.
augment int get x => augmented + 1;
augment set x(int value) => augmented = value - 1;

// Second level of augmenting declarations.
augment int x = augmented + 2;

The following sentence in the specification justifies this situation, in the section about 'Augmenting a getter and/or setter with a variable':

If a non-abstract, non-external variable is augmented by an augmenting getter or setter, you can still augment the variable, as you are only augmenting the initializer of the original variable. This is not considered to be augmenting the augmenting getter or setter, since those are not actually altered.

My reading of this would be that the variable x is initialized to have the value 3, and an evaluation of x would then yield 4. In other words, augmented at the second level of augmentation refers to the original, not the first level of augmentation.

Isn't that somewhat surprising? :grinning:

If we do continue to allow this then we should probably also allow adding a DartDoc comment and/or metadata (#3957).

jakemac53 commented 4 days ago

You do read it correctly. It might be a bit weird, but I think it is also fairly easy to rationalize/explain.

I do also think we should allow doc comments and metadata.

lrhn commented 4 days ago

Agree that it can be explained. You can augment either the implicit getter, the implicit setter or the initializer expressions. Those are distinct entities, and are augmented independently.