dart-lang / dart-lang/language
Proposal: operator !! (opposite of ??)
- Dominant language
- TeX
- Stars
- 2.9k
- Forks
- 239
- Avg merge
- 2d 18h
- Merged PRs (30d)
- 14
Description
Proposed solution to #360.
Where `a ?? b` is sugar for `a != null ? a : b`, there may be value in adding `!!` defined as `a == null ? null : b`.
Example: calling a function when the parameter is not null.
```dart
String? toPrint;
toPrint !! print(toPrint);
```
Example: awaiting a future when its not null
```dart
Future? fut;
fut !! await fut;
```
Example: asserting an object is fully initialized
```dart
assert(name !! age !! email !! password != null)
```
Similar to #219 except I think it has a few different advantages:
- extremely simple and consistent with an existing well-used and understood dart concept
- binary operator so left-to-right flow is easier to follow
- wouldn't interact with any other syntax (prefix ? is was proposed to interact with maps, lists, optional parameters, etc.)
Disadvantages:
- using `!` and not throwing an exception on null is confusing. Maybe `?!` or `!?`?
- Limited usefulness
- repeat lhs required to take advantage of promotion
Contributor guide
Research direction
Read this proposal, then follow #360 and #219 and the 18-comment thread to understand the unresolved syntax and usefulness questions. No implementation files or tests are named; the work is only ready when the language design is decided and its specification and implementation touchpoints are identified.
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