dart-lang / dart-lang/language
The `final` keyword is too long
- Dominant language
- TeX
- Stars
- 2.9k
- Forks
- 239
- Avg merge
- 2d 18h
- Merged PRs (30d)
- 14
Description
It's inconvenient that declaring final variables require more typing than non-final variables. This discourages the use of final variables.
Even with inferred types, it's shorter, and if you want to specify the type, it gets even longer:
```dart
var foo = ...;
String foo = ...;
final foo = ...;
final String foo = ...;
```
While final variables are not strictly necessary (if you don't assign to a variable, it doesn't matter semantically whether it's declared final or not), some people prefer a style where you make variables final unless they need to be mutable, and Dart does not support that writing style very well.
See #135 for one suggestion which improves the experience for inferred variables by replacing `final` with `val`. It doesn't improve the typed case. It requires adding a new built-in identifier (but likely not a reserved word).
Another option could be using `:=` for final initialization (since final local variables need to be initialized):
```dart:
foo := ...;
String foo := ...;
```
This doesn't work for instance variables since they may be intialized by a constructor, or parameters, and to keep requiring the `final` word for instance variables and parameters, and using `:=` everywhere else, is inconsistent.
Contributor guide
Assessment
This issue has not been assessed yet.