dart-lang / dart-lang/language

Make `MyEnum.aValue.anInstanceVariable` constant

Open
#2,780 11 comments 29 reactions 0 assignees View on GitHub
enhanced-const small-feature
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

![Image 2023-1-18 at 9 42 PM](https://user-images.githubusercontent.com/52090518/213290824-f35686c8-6a94-4df9-870a-3f6968872361.jpg)

I am using:

- Dart SDK version: 2.19.0-444.4.beta
- Android Studio on MacOSX

Contributor guide

Open the contributing 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.