dart-lang / dart-lang/language

Request or suggestion for a!++ or a!+=x; syntax.

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

Description

As in the title, ofcourse there are only rare cases it could be used. Some respected user from Discord Dart official channel suggested i might make a request.
Here are some examples that i originally put on the discord channel:
```dart
c(int? a) {
// WATCH OUT: This is a very simplified function - i know a is not null
a! += 10; // error Missing selector such as '.identifier' or '[0]'.
a = a + 10; // overflow info: when the previous line exists this one doesn't show error;
}

d(int? a) {
a = a + 10; // error: The operator '+' can't be unconditionally invoked because the receiver can be 'null'. Try adding a null check to the target ('!').
}

// For comparison this, ofcourse, is fine - no a! += 10; way
e(int? a) {
a = a! + 10; // ofcourse fine.
}
```

Then the notable discord user suggested i could even do this:
```dart
void c(int? a) {
a!;
a += 10;
}
```
After the suggestion he wrote this:
"I am not sure how useful this actually is in real-world scenarios since we should rarely need to do ! in our code, but should instead let Dart type promote our null-able variable so we can safely do those math operations.

But sure, there are cases where this could be needed (e.g. public nullable properties on objects)".

Contributor guide

Open the contributing guide

Research direction

Review the Dart examples in the issue and determine the intended semantics of postfix null assertions combined with compound assignment. No source files or tests are named; done would require a settled language-design decision and defined specification, implementation, and test work.

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.