Skip to content

Allow augmentation of getters and setters with a larger set of possible implementations #4623

@eernstg

Description

@eernstg

Consider the following program:

class A {
  get v01;
  get v02;
  set v02(_);
  int get v03;
  set v03(int _);
  static get v04;
  static get v05;
  static set v05(_);
  static int get v06;
  static set v06(int _);
  abstract covariant var v07;
  abstract covariant int v08;
  static const v09; // Can not be augmented.
  static const int v10; // Can not be augmented.
  static get v11; 
  static int get v12;
  static get v13;
  static set v13(_);
  static int get v14;
  static set v14(int _);
  get v15;
  set v15(covariant _);
  int get v16;
  set v16(covariant int _);
  get v17;
  set v17(covariant _);
  int get v18;
  set v18(covariant int _);
  get v19;
  int get v20;
  get v21;
  set v21(_);
  int get v22;
  set v22(int _);
  get v23;
  set v23(_);
  int? get v24;
  set v24(int? _);
  get v25;
  set v25(_);
  int get v26;
  set v26(int _);
}

augment class A {
  augment external final v01;
  augment external var v02;
  augment external int v03;
  augment external static final v04;
  augment external static var v05;
  augment external static int v06;
  augment external covariant var v07;
  augment external covariant int v08;
  // augment static const v09; // Error, cannot augment constant variables.
  // augment static const int v10; // Error, cannot augment constant variables.
  augment static final v11 = null;
  augment static final int v12 = 0;
  augment static late final v13;
  augment static late final int v14;
  augment covariant late final v15;
  augment covariant late final int v16;
  augment covariant late var v17; 
  augment covariant late int v18;
  augment final v19 = null;
  augment final int v20 = 0;
  augment late final v21;
  augment late final int v22;
  augment var v23;
  augment int? v24;
  augment late var v25;
  augment late int v26;
}

This program shows a number of augmenting declarations involving getters, setters, and variables. All declarations starting with augment are syntax errors according to the augmentations feature specification. However, I think all of them are meaningful, and it may be a natural expectation that they can be used.

If we are going to allow them then we'd need to generalize the grammar slightly (in particular, by adding 'augment'? to almost all grammar rules in the rule for <declaration>).

To take an example, consider augment covariant late final int v16;. This declaration provides an implementation of a getter with signature int get v16 and a setter with signature set v16(covariant int _) (note that it does induce a setter even though it is final, because it is late and has no initializing expression).

The corresponding introductory declarations are int get v16; and set v16(covariant int _); (and we could actually omit covariant). Should work, I think.

@dart-lang/language-team, WDYT?

Metadata

Metadata

Assignees

No one assigned

    Labels

    augmentationsIssues related to the augmentations proposal.questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions