dart-lang / dart-lang/language

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

Open
#4,050 2 comments 0 reactions 0 assignees View on GitHub
static-extensions
Dominant language
TeX
Stars
2.9k
Forks
239
Avg merge
2d 18h
Merged PRs (30d)
14

Description

Mixin declarations currently may not declare factory constructors:

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

Both variants of the static extensions proposal in [this PR](https://github.com/dart-lang/language/pull/3835) 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:

```dart
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

Contributor guide

Open the contributing guide

Research direction

Start by reading the static extensions proposal in PR 3835 and compare its implications with the existing restriction on factory constructors in mixin declarations. Determine whether the behavior is intentional and whether the language specification needs a design decision or clarification. Done means the restriction and the interaction with static extensions are explicitly settled.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.