Skip to content

[Static extensions] Can static extensions declare constructors with prefixed names? #4681

Description

@leafpetersen

The proposal for adding constructors to static extensions is currently (I believe) in a somewhat inconsistent state with respect to declaring constructors in extensions for which the on-declaration is a prefixed name. The text possibly implies that it may be possible to declare such a constructor using a prefixed name, however, I believe that the syntax does not yet support such a declaration, and also the text around invocations I believe would need some modification to account for this. Consider the following examples:

import "foo.dart" as f; // foo.dart declares a class named C and a class named f
import "foo.dart" show D; // foo.dart also declares a class named C

extension E1 on f.C {
  f.C.name(); // Is this a valid declaration of a constructor named `C.name`?
}

extension E2 on f.C {
  C.name(); // Is this a valid declaration of a constructor named `C.name`?
}

extension E2 on f.f {
  f.f(); // Is this a valid declaration of a constructor named `f.f`?  Or of a constructor named `f.new`?
}

extension E4 on D {
  f.D.name(); // Is this a valid declaration of a constructor named `D.name`?
}

What do we intend to support here?

cc @dart-lang/language-team

Activity

  1. self-assigned this
    on Apr 14, 2026
  2. lrhn commented on Apr 15, 2026

    @lrhn
    Member

    I'd say "none of the above", and only support new nameless constructor syntax, new(), new.id(), const() or const.id().

    That should avoid any issue.

    Otherwise I'd probably go with E2. The name of the class in the constructor declaration is not a scope lookup, it's "the same name as the surrounding class".
    The name of the class being extended is C.
    Or maybe it isn't, you can make an extension on a public alias of a privately named class.

    The only way to win is not to play. Just don't write a name

  3. leafpetersen commented on Apr 15, 2026

    @leafpetersen
    MemberAuthor

    We talked about this in the meeting, we will do what Lasse proposes above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

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