dart-lang / dart-lang/language
Make `MyEnum.aValue.anInstanceVariable` constant
- Dominant language
- TeX
- Stars
- 2.9k
- Forks
- 239
- Avg merge
- 2d 18h
- Merged PRs (30d)
- 14
Description
I have noticed that dart won't accept the argument of enums as const value, although both enums and their arguments are exclusively const.
This really sucks when trying to keep the class constant, but just by accessing the definitely constant argument the class won't be accepted as a constant one.
```dart
enum Foo {
bar('The answer to life, the universe, and everything.'),
baz('Flutter and dart are awesome.');
const Foo(this.message);
final String message;
}
class Bar {
final Foo foo;
const Bar(this.foo)
: assert(foo == Foo.bar),
assert(foo.message == "Only real messages"); // Will not accept message as const
}
```
Screenshot in Android studio

I am using:
- Dart SDK version: 2.19.0-444.4.beta
- Android Studio on MacOSX
Contributor guide
Research direction
Start with the enum and const-constructor reproducer in the issue using the listed Dart SDK version, then investigate how enum instance-variable access is treated in constant expressions. Done means the shown Bar declaration accepts foo.message in a const assertion without losing const validity, with the language behavior documented or tested.
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