dart-lang / dart-lang/language
Add modifier for static variables and methods to be inherited by all subclasses.
- Dominant language
- TeX
- Stars
- 2.9k
- Forks
- 239
- Avg merge
- 2d 18h
- Merged PRs (30d)
- 14
Description
Static variables do not pass thru in the inheritance like instance variables. Not sure why Dart architecture does it like that. I know for example in Smalltalk ClassVariableNames are passed thru (via methods) automatically. However I believe, in Dart, this is a limitation in that to create a subclass, one must also implement access to static variables:
E.g.
```dart
// Parent widget with static variable and method
class PWidget {
static int height = 5;
static _width = 10;
static int get width => _width;
static set width(int value) {
_width=value;
}
}
// Child Widget. However static of parent needs to be reimplemented to be accessible.
class CWidget extends PWidget {
static int height = PWidget.height;
}
main() {
print(PWidget.height);
print(CWidget.height); // works because it is implemented in in child class
}
```
This imposes a responsibility on someone creating a subclass to reimplement all the static variables so that they are accessible by the user of the subclass. A simpler way of course would be to pass thru the static variable and methods to the child class, without the need to reimplement.
I propose to give the designer the ability to subclass all static parent class variables AND methods using a static modifier on the class creation. E.g.
```dart
// Parent widget
class PWidget {
static int height = 5;
static _width = 10;
static int get width => _width;
static set width(int value) {
_width=value;
}
}
// Child Widget extends PWidget with a static modifer
class CWidget extends PWidget static {
}
main() {
print(PWidget.height);
print(CWidget.height);
print(CWidget.width);
CWidget.width = 50;
}
```
Contributor guide
Assessment
This issue has not been assessed yet.