language icon indicating copy to clipboard operation
language copied to clipboard

How can we augment an abstract variable?

Open eernstg opened this issue 6 months ago • 18 comments

tl;dr   Allow augmenting an abstract variable by a concrete getter and/or setter, or another abstract variable   rd;lt

The augmentation specification has the following rules:

An abstract variable declaration is equivalent to an abstract getter declaration, and if not final, also an abstract setter declaration.

... abstract variables cannot augment their initializing expression, since it does not exist ...

It is a compile-time error if:

  • ...
  • An abstract variable is augmented with a non-abstract variable, getter, or setter.

Note that there's nothing new about the first rule, this is also the way abstract variables are specified in the feature specification. So it makes sense to expect that an abstract variable should be treated in exactly the same way as the corresponding abstract getter/setter pair, or at least very similarly.

Does this mean that we cannot augment an abstract variable declaration at all .. other than by another abstract variable, or an abstract getter and/or setter, e.g., in order to add metadata or DartDoc comment?

This seems to be unnecessarily strict: If the abstract variable is indeed equivalent to an abstract getter and possibly an abstract setter then we should be allowed to augment the getter and/or setter just like any other getter and/or setter. We do have this, in the section about augmentation of getters and setters:

The augmenting declarations can themselves be an implicitly induced getter or setter, or an explicitly declared one.

We may even be allowed to augment the abstract variable with another variable declaration:

Augmenting a variable with a variable: ... [not an error] ...

We should be able to use this to add metadata or DartDoc comments.

This is more than we can do with explicitly specified getters and setters:

Augmenting a getter and/or setter with a variable: This is a compile-time error in all cases.

It was mentioned (in commentary) that we cannot augment the initializing expression, but this is consistent with the perspective that the abstract variable is (precisely) an abstract getter and possibly an abstract setter, so it makes sense that we cannot augment the storage part of a (non-existing) variable.

In summary, I believe we should change the compile-time error mentioned above to the following:

It is a *compile-time error if:

  • ...
  • An abstract variable is augmented with a non-abstract variable.

@dart-lang/language-team, WDYT?

eernstg avatar Aug 06 '24 15:08 eernstg