Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Static Extensions] Can static extensions add constructors to mixins? #4050

Open
leafpetersen opened this issue Aug 22, 2024 · 2 comments
Open
Labels
static-extensions Issues about the static-extensions feature

Comments

@leafpetersen
Copy link
Member

Mixin declarations currently may not declare factory constructors:

mixin M {
  // This is an error
  factory M.foo() => throw "hello";
}

Both variants of the static extensions proposal in this PR imply that static extensions can be used to add factory constructors to mixin declarations (am I missing a restriction or error somewhere?). That is, the following would be legal:

mixin M {
}

extension on M {
  factory M.foo() => throw "hello";
}

Is this intentional? It seems slightly unfortunate that using an extension becomes "the way" to get a constructor onto a mixin. Should we relax this restriction on mixin declarations?

cc @dart-lang/language-team

@leafpetersen leafpetersen added the static-extensions Issues about the static-extensions feature label Aug 22, 2024
@natebosch
Copy link
Member

There is little practical difference between static M foo() => throw 'hello'; and factory M.foo() => throw 'hello';. Do we plan to allow a static extension to define the unnamed constructor? That's one affordance that might still only be open if we allow mixins to define factory constructors.

I don't see any harm in relaxing the existing restriction.

@lrhn
Copy link
Member

lrhn commented Aug 24, 2024

Mixin declarations currently may not declare factory constructors

That's a long-standing missing feature. There was never any reason to disallow factory constructors on mixin declarations, and it should just be allowed.

We discussed that with class modifiers too, but to late in the process to want to add more to the feature.

So yes, relax the restriction. (Finally!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
static-extensions Issues about the static-extensions feature
Projects
None yet
Development

No branches or pull requests

3 participants