dart-lang / dart-lang/language

Partial Record destructuring

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

Description

## Problem:

Currently, it is not possible to use destructuring to only extract one or two variables out of a Record.
Consider a large Record:

```dart
typedef Big = ({ int field1, int field2, ...., int field8 });
```

If we want to extract `field1`+`field2` but nothing else, we currently have to write:

```dart
void main() {
final Big big = (field1: 42, field2: 42, field8: 42);
final (:field1, :field2, field3: _, field4: _, ..., field8: _) = big;
}
```

That's bloody inconvenient. It's both really tedious to type, difficult to read (due to a lot of noise), and difficult to maintain.
In particular, adding a new field in that Record requires going through **every** places where it was destructured.

## Proposal:

When destructuring is used inside variable declarations, we could make specifying any field optional.
Using the previous `Big` record, we could therefore write:

```dart
void main() {
final Big big = (field1: 42, field2: 42, field8: 42);
final (:field1, :field2) = big;
}
```

This is how Record types work in Typescript.

Contributor guide

Open the contributing guide

Research direction

Start with the Problem and Proposal examples in this issue, then read the 12-comment discussion for existing objections or decisions. No files, tests, or entry points are named; the work is done only when the semantics and required language-specification changes for partial Record destructuring are agreed.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.