dart-lang / dart-lang/language
[nnbd] Late Keyword is Redundant for Local Non-nullable variables without Intializers
- Dominant language
- TeX
- Stars
- 2.9k
- Forks
- 239
- Avg merge
- 2d 18h
- Merged PRs (30d)
- 14
Description
# Late assignment of non-nullable local variables
```dart
void main() {
int i;
we();
i = 42;
}
```
The above is **strictly equivalent** to the following:
```dart
void main() {
late int i;
we();
i = 42;
}
```
This is because non-nullable variables also need to be assigned before first access. For nullable variables, the above does not apply because they can be accessed without explicit initialization.
### Late lazy
Obviously, the `late` modifier is still useful for non-nullable local variables when they have initializers.
```dart
void main() {
late int i = calculate();
// w/e
i;
}
```
This would be an argument for separating the keyword into two distinct modifiers (which is already the case under the hood as the two effects of the modifier are mutually exclusive).
---
## Proposal
I think that any one of these actions is sensible given the above:
* Do not allow `late` for non-nullable variables without initializers.
* Separate the `late` modifier into two keywords, i.e. `late lazy` and `late assign`.
* Make a lint for this being redundant.
## Why?
To me the two effects, or better two _kinds_, of the `late` modifier make perfect sense, however, I think that interactions like this will make `late` very confusing for people who spend less time with Dart.
### Related
#1100
Contributor guide
Research direction
Start by reviewing the proposal and examples in issue #1101, then read the related issue #1100 for surrounding context. Compare the three proposed directions—rejecting redundant late, splitting modifiers, or adding a lint—and determine which language behavior the project should adopt; the issue does not identify a selected implementation or test location.
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
- Needs clarification
- Newbie friendliness
- 25/100