Skip to content

Allow an extension to provide the missing part of a getter/setter pairΒ #4334

Open
@eernstg

Description

@eernstg

We could allow a getter/setter pair to consist of one member declared for the receiver type (as a member of a class, mixin, mixin class, enum, or extension type) and the other member declared by an applicable extension.

See dart-lang/web#357 for a discussion which motivates this proposal. In that issue, the point is that a JS interop extension type needs to have an operator [], and we wish to use an extension to provide support for an operator []= ([]/[]= are considered to be a getter/setter pair). This fails to work because the operator []= in the extension is shadowed by the operator [] in the receiver type (when we have found the "getter", we don't even try to look for the "setter" in an extension, and vice versa). For example:

extension on A {
  set x(int _) {}
}

class A {
  int get x => 0;
}

void main() {
  A().x = 0; // Error today, OK with this proposal.
}

It seems both useful and benign (in particular: non-breaking) to allow extensions to provide the missing part of a getter/setter pair.

The corresponding specification change could be achieved as follows. The following phrase:

(2) the interface of the static type of $r$ does not have a member
whose basename is the basename of $m$,

should be adjusted as follows:

(2) the interface of the static type of $r$ does not have a member named $m$,

[Edit, April 28th: The above wording won't quite work, we need to prevent the case where the receiver type has a setter and the extension has a method, and vice versa. This could give rise to some slightly bigger changes. Thanks to @lrhn for noticing this issue.]

@dart-lang/language-team, WDYT?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions