dart-lang / dart-lang/language
Non-growable list literal syntax
- Dominant language
- TeX
- Stars
- 2.9k
- Forks
- 239
- Avg merge
- 2d 18h
- Merged PRs (30d)
- 14
Description
Non-growable lists are implemented more efficiently than growable lists, but creating one is rather verbose and inconvenient.
```dart
// Growable:
var items = foo.map(toItem).toList();
// Non-growable:
var items = foo.map(toItem).toList(growable: false);
// Growable:
var items = [
...firstThings.map((e) => e.foo),
...secondThings.map((e) => e.foo),
if (condition) lastThing,
];
// Non-growable:
var intermediate1 = firstThings.map((e) => e.foo);
var intermediate2 = secondThings.map((e) => e.foo);
// Oops, we cannot use List() any more.
var items = List(intermediate1.length + intermediate2.length + 1);
// Concatenate the Iterables?
var items = List.of(
intermediate1.followedBy(intermediate2).followedBy([lastThing]),
growable: false);
```
Oh! to give up our precious and novel UI-as-code, our spreads and for-elements and if-elements... ☹️ And we're forced to create an intermediate 1-element list with `followedBy([lastThing])`, and I didn't even implement the `if (condition)` part.
### Solution
What a beautiful, space-efficient, fun and quirky world we'd live in, if we had a non-growable list literal syntax. I'm thinking:
```dart
// growable: true
var items = foo.map(toItem).toList();
// growable: false
var items = [...foo.map(toItem)]gf;
// Growable:
var items = [
...firstThings.map((e) => e.foo),
...secondThings.map((e) => e.foo),
if (condition) lastThing,
];
// Non-growable:
var items = [
...firstThings.map((e) => e.foo),
...secondThings.map((e) => e.foo),
if (condition) lastThing,
]gf;
```
Or if we want to reserve `gf` for a gluten-free option, we can use `[]ng` ("non-growable"), `[]f` ("fixed-length"), whatever. A preceding `f` would be great (`f[]`), but I can't see how that wouldn't blatantly conflict with index-expressions.
Contributor guide
Research direction
Start with issue #2477 and read the full comment discussion, focusing on the proposed non-growable list literal forms and their interaction with spreads, for-elements, and if-elements. Done would require an agreed syntax and corresponding Dart language-design decision; no implementation files or tests are identified in the payload.
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
- 35/100