dart-lang / dart-lang/language
Allow shadowing local final variables
- Dominant language
- TeX
- Stars
- 2.9k
- Forks
- 239
- Avg merge
- 2d 18h
- Merged PRs (30d)
- 14
Description
With Dart 3.0 it is now more viable than ever to write functional-style code. However the lack of support for shadowing local variable makes it problematic to write code like
```dart
final value = SomeThing();
final value = transform(value);
final value = otherTransform(value);
```
Traditionally shadowing local variable in same scope may seem a bit contentious, but it is used in functional languages or even Rust. Right now after every transform it is necessary to come up with a separate name, with all previous values being in scope, which adds some friction and can be error-prone:
```dart
final value = SomeThing();
final valueTransformed = transform(value);
final valueTransformed2 = otherTransform(valueTransformed);
// ...
valueTransformed.doSomething(); // should have been valueTransformed2
```
I think it would be acceptable if this was only possible for final bindings. Shadowing rebindable variables could still be an error.
EDIT: Possibly a lint or an error could be introduced where the shadowing variable would need to reference the original variable in the initializer:
```dart
final value = getBytes();
final value = sanitize(value); // works
final value = decode(value); // works
final value = b; // fails
```
This would would make it possible to use shadowing during transformations, but would prevent redefining the variable by accident.
Contributor guide
Research direction
Review the issue's Dart 3.0 examples and the discussion about final bindings, rebindable variables, and initializer references. Determine the language-design rules needed for same-scope shadowing and how accidental redefinitions should be rejected. Done means the specification behavior and diagnostic conditions are resolved.
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