dart-lang / dart-lang/language

Overriding fields to be final

Open
#1,390 13 comments 5 reactions 0 assignees View on GitHub
feature
Dominant language
TeX
Stars
2.9k
Forks
239
Avg merge
2d 18h
Merged PRs (30d)
14

Description

Consider this code:
```dart
class A {
bool _temp;
bool get temp => _temp;
set temp(bool value) {
print("Changing A.temp to $value");
_temp = value;
}

A(this._temp);
}

class B extends A {
@override
final bool temp;

B(this.temp) : super(temp);
}

void main() {
final A a = A(true);
a.temp = false; // "Changing A.temp to false"
print(a.temp); // false

final B b = B(true);
b.temp = false; // "Changing A.temp to false"
print(b.temp); // true
}
```

Based on the print statements, it seems that Dart is referring to A's setter because B doesn't have one. I can confirm this because when I changed B.temp from final to a getter and setter like A has, B's print statements ran instead. But this behavior is unintuitive, and I would expect this to either error or B's getter to return whatever A's setter received.

Contributor guide

Open the contributing guide

Research direction

Start with the Dart example in the issue and reproduce the interaction between B's final field and A's setter. Review the language rules governing overridden fields, getters, and setters, then determine which of the two proposed behaviors—an error or a getter reflecting the setter's value—is intended as the completed outcome.

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
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.