dart-lang / dart-lang/language

Add possibility to access Enum values by its Type

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

Description

Currently we can access enum values in such a way:
```dart
enum Foo {
bar,baz
}

void main() {
print(Foo.values);
}
```

But this becomes problem when working with generics:
```dart
E findEnum(final String toString) {
List values = []; //?How to get the values from E?
return values.firstWhere((element) => element.name == toString);
}
```

As far as I know, all enum values are known at compilation time, so perhaps it could be possible to access this list by `E.values`?

Contributor guide

Open the contributing guide

Research direction

Start with the generic findEnum example and the current Foo.values behavior described in the issue. No files, tests, or entry points are named; done would require determining whether type-based access to enum values belongs in the Dart language design and documenting the resulting specification or decision.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.