dart-lang / dart-lang/language

Support ??= as default value operator

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

Description

Hello!

Currently when using default value, we do:

```dart
MyClass({this.number = 42});
```

This works by relying on whether the parameter was passed or not.
But a common alternative is to not rely on whether is passed or not, but whether it is null or not:

```dart
class MyClass {
MyClass({int? number})
: number ??= 42;

final int number;
}
```

This can be quite tedious to use in part because it doesn't work with `this.value`/`super.value`, so we can't infer the type and have to type much more than needed.

Would it be possible to simplify this pattern with:

```dart
class MyClass {
MyClass({this.number ??= 42});

final int number;
}
```

Of course, the default value could be non-constant in this case. And the `number` parameter would be made nullable even if the property associated is non-nullable.

Contributor guide

Open the contributing guide

Research direction

The issue provides no repository files, tests, or entry points. Start by reviewing the Dart language-design proposal and discussion; done would require an accepted specification for ??= in formal parameters, including nullability and non-constant defaults.

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
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.