dart-lang / dart-lang/language

`enum`s extending other types

Open
#3,780 5 comments 7 reactions 0 assignees View on GitHub
feature
Dominant language
TeX
Stars
2.9k
Forks
239
Avg merge
2d 18h
Merged PRs (30d)
14

Description

#### Proposal

Allow the following as valid dart code:

```dart
enum MyEnum extends Foo { ... }
```



My understanding is that the `enum` keyword desugars to something like

```dart
class MyEnum extends _Enum { ... }
```

when instead, it could be

```dart
@EnumMacro()
class MyEnum implements Enum { ... }
```



With this change, you could make enumerated collections for widgets, colors, decorations, text styles… anything with a `const` constructor!

```diff
- enum NavBarButton {
+ enum NavBarButton extends StatelessWidget {
home(Icons.home),
leaderboard(Icons.leaderboard),
schedule(Icons.calendar_month),
profile(Icons.account_circle);

final IconData icon;
const NavBarButton(this.icon);

+ @override
- Widget get destination {
+ Widget build(BuildContext context) {
return NavigationDestination(icon: Icon(icon), label: name);
}
}
```

#### before

```dart
NavigationBar(destinations: [for (final button in NavBarButton.values) button.destination]);
```

#### after

```dart
NavigationBar(destinations: NavBarButton.values);
```


I'd love to see this implemented as part of the [code generation](https://github.com/dart-lang/language/issues/1565) release.

Contributor guide

Open the contributing guide

Research direction

Start by reading the proposal and the linked code-generation issue (#1565), then review how Dart currently desugars enum declarations. Done means the shown enum-extending examples are valid Dart and the resulting values can be used as the demonstrated widgets or other const-constructible types.

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
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.