dart-lang / dart-lang/language

Proposal: Allow "late final" on const classes and enhanced enums

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

Description

Today, it is unintuitive how to support lazy/memoized fields on const classes or enhanced enums:

```dart
enum Ansi8BitColors {
black,
red,
green,
yellow,
blue,
magenta,
cyan,
white;

// This is a simple example, imagine something more runtime/memory intensive.
late final escapeCode = '\u001b[${index + 30}';
}
```

The most obvious pattern to me is using an expando:

```diff
enum Ansi8BitColors {
black,
red,
green,
yellow,
blue,
magenta,
cyan,
white;

+ static final _escapeCodes = Expando();
- late final escapeCode = '\u001b[${index + 30}';
+ String get escapeCode => _escapeCodes[this] ??= '\u001b[${index + 30}';
}
```

However, that just seems unnecessary. Could const classes/enhanced enums just support this out of the box?

Contributor guide

Open the contributing guide

Research direction

Start by reading the proposal and its enum example, then review the surrounding Dart language rules for const classes, enhanced enums, and late final fields. Done means the language-design question has a decided specification outcome and the documented behavior covers the proposed lazy or memoized fields.

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
Clearly specified
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.