dart-lang / dart-lang/language
Provide an expression to maybe provide an optional parameter
- Dominant language
- TeX
- Stars
- 2.9k
- Forks
- 239
- Avg merge
- 2d 18h
- Merged PRs (30d)
- 14
Description
As discussed on [Discord](https://discord.com/channels/608014603317936148/684483384839503894/798337137933549628) there is no short solution to **apply an optional parameter *maybe* a value using an expression**:
```dart
//@dart=2.12
class Sample {
const Sample({this.a = 42});
final int a;
}
Sample someFunction() {
int? value;
// ...
// calculations do potentially set value
// ...
// **The following line is not valid in Dart >= 2.12**,
// but the *expected / hoped for* output would be
// `value != null ? Sample(a: value) : Sample()`
return Sample(a: value);
// # Work-arounds I see currently
return Sample(a: value ?? 42);
// Means you hardcode a default that might change over time
return value != null ? Sample(a: value) : Sample();
// In this sample okayish, but imagine `Sample` was a `Widget`
// where you set ~10 named parameters, each potentially being an expression again
// # Something I thought might potentially work, but does not 🙊
// I guess this is very wrong from a grammar/semantics PoV
return Sample(a: value ?? Never);
}
```
**The same issue should apply for optional positional parameters.**
Did not have the time yet to fully read on the [Pattern language feature draft](https://github.com/dart-lang/language/blob/master/working/0546-patterns/patterns-feature-specification.md), but I can imagine that there might be some ”*connection*“ in the long run.
Contributor guide
Research direction
Start with the examples and workarounds in this issue, then read the linked Pattern language feature draft. Compare the requested behavior for optional named and positional parameters with Dart's current grammar and semantics. Done means a resolved language-design proposal that specifies the behavior and its relationship to existing features.
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