dart-lang / dart-lang/language
Provide a way to "unzip" nullability by destructring a nullable record of values into a non-nullable record of nullable values
- Dominant language
- TeX
- Stars
- 2.9k
- Forks
- 239
- Avg merge
- 2d 18h
- Merged PRs (30d)
- 14
Description
Consider the following code:
```dart
void main() {
final (a, b) = getIntegers();
}
(int, int)? getIntegers() => (0, 0);
```
The code generates the following error:
```
error: The matched value of type '(int, int)?' isn't assignable to the required type '(Object?, Object?)'. (pattern_type_mismatch_in_irrefutable_context)
```
This can be solved by many ways, like providing a default value, using bang or not using destructure:
```dart
void main() {
// Option 1
final (a, b) = getIntegers() ?? (0, 0);
// Option 2
final (a, b) = getIntegers()!;
// Option 3
final integers = getIntegers();
final a = integers.$1 ?? 0;
final b = integers.$2 ?? 0;
}
(int, int)? getIntegers() => (0, 0);
```
However, sometimes we want to have the destructured values to be nullable when the record value is nullable. It would be useful to have a way to do this while destructuring. It would work like a method `unzip` on a functor `F (a b) => (F a, F b)`).
Example:
```dart
void main() {
final (a?, b?) = getIntegers(); // a and b are int? instead of int, no errors
}
(int, int)? getIntegers() => (0, 0);
```
Contributor guide
Research direction
Start with the nullable record and destructuring examples in issue #3533, then review the existing pattern type-mismatch behavior described there. Define the proposed syntax and nullability semantics, including how the examples should type-check and what happens when the record is null. Done means the language proposal and resulting diagnostics are specified clearly.
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
- 30/100