Skip to content

Allow read-only access to initializing formals. #26655

Closed
@eernstg

Description

@eernstg

For convenience, and because it is non-disruptive, we wish to support read-only access to initializing formal arguments, that is, the this.x style arguments that generative constructors may have. An example:

class C {
  final int x;
  final int y;
  C(this.x) : y = x + 1 {
    int z = x + 2;
    assert(z == y + 1);
  }
  const C.constant(this.x) : y = x + 1;
}

The details of this language change are as follows:

  • The grammar is unchanged.
  • An initializing formal this.x of a constructor C introduces the name x into a scope that covers C's initializers, but not the body; it is considered to be a final parameter.
  • The semantics of such an initializing formal access is that it accesses the parameter, not the field. This matters because the initializing formal may be captured by a function expression, and the field may be updated.

The feature is currently available in dart2js under the flag --initializing-formal-access (which will be removed such that the feature is available in general), and some tests are available in the files tests/language/initializing_formal_*_test.dart.

Tracking bugs for the individual tasks:

There are no tasks for the formatter and style guide because this change does not affect the syntax.

Edit: Note that the scope rule was changed such that the initializing formal is not in scope in the body, which means that existing usages of the field in the body will preserve their current semantics.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-languageDart language related items (some items might be better tracked at github.com/dart-lang/language).area-metaCross-cutting, high-level issues (for tracking many other implementation issues, ...).

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions