dart-lang / dart-lang/language

Record spreading

Open
#2,128 17 comments 105 reactions 0 assignees View on GitHub
patterns records
Dominant language
TeX
Stars
2.9k
Forks
239
Avg merge
2d 18h
Merged PRs (30d)
14

Description

The current [records proposal](https://github.com/dart-lang/language/blob/master/working/0546-patterns/records-feature-specification.md#records-feature-specification) does not introduce a "spreading" operator.
I think it should have one.

A "record spread", with syntax `... recordExpression`, would be allowed inside any record expression or argument list.
The `recordExpression` must have a static type which is an actual record type (not `dynamic` and not `Record` or `T extends Record`).

It works by, effectively, inlining every member of the tuple at the spread-point. So,
```dart
(num, num) point = ...;
(num, num, {Color color}) colorPoint = (...point, color: Color.red};
```
would inline the elements of `point` as elements of the new record at the point of the spread.

It would also work in argument lists.

This is a *static* operation which requires knowing the type of the spreadee and allows knowing the structure of the result.
It won't allow something like
```dart
Record concat(Record a, Record b) => (...a, ...b); // Not allowed!
```
because neither `a` nor `b` have known structures.

That also means that it can be desugared into, e.g, `(point[0], point[1], color: Color.red)` if we have a syntax for directly accessing members of a record. (If we don't, the desugaring would include a pattern match of some sort, and might not be directly expressible as an expression in the language.)

Contributor guide

Open the contributing guide

Research direction

Start by reading working/0546-patterns/records-feature-specification.md and the record-spreading proposal in this issue. Compare the proposed syntax, static type restrictions, record expressions, and argument lists with the existing records specification; done requires a decided language design and corresponding specification treatment, if accepted.

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
Quiet
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.