Skip to content

Can generative constructors declared in static extensions be used as targets of redirecting constructors? #4559

@leafpetersen

Description

@leafpetersen

The current spec allows static extensions to declare generative redirecting constructors, but forbids their use as the target of a redirection. As discussed here, this means the only reason to use them is to allow for a const constructor. Moreover, replacing a normal constructor with one declared in an extension becomes more breaking than it otherwise need be.

The motivation for this restriction is, I believe, largely based on the issue illustrated in this example:

class S<T> {
  T x;
  S(this.x);
  S.redirecting() : this.named();
}

extension on S<int> {
  S.named() : this(3);
}

The S.named constructor declared in an extension always returns a specific instance of S, which makes it unsafe/invalid to use as the target of redirection in the generic S.redirecting constructor.

This seems easily remedied by adding an text specifying that it is an error if the target of a redirecting constructor has a static type which is not a subtype of the type of the enclosing class. Is this sufficient? Are there other soundness issues lurking here?

There is now an inference problem to solve at the redirection point, but this seems likely to be easily dealt with in the same manner as with redirecting factory constructors.

cc @dart-lang/language-team

Metadata

Metadata

Assignees

No one assigned

    Labels

    static-extensionsIssues about the static-extensions feature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions