Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the rationale behind not inheriting static variables, in Dart?

In Dart, if one class extends another, the extended class inherits all of the super classes non-static variables, but inherits none of its static variables.

For example

class TestUpper {
  static final String up = 'super';
  String upup = 10;
}

class TestLower extends TestUpper {
  static final String low = 'lower';
  String lowlow = 11;
} 

var lower = new TestLower();
print( lower.lowlow );  // <== 11
print( lower.upup );    // <== 10
print( TestLower.low ); // <== "lower"
print( TestLower.up );  // <== No static getter 'get:up' declared in class 'TestLower'

Is this the normal behavior? If so, I would appreciate if someone explained the rationale behind it.

like image 582
cc young Avatar asked Nov 26 '13 11:11

cc young


1 Answers

Yes, there's no inheritance of static members. See Static Methods section of the language specification :

Inheritance of static methods has little utility in Dart. Static methods cannot be overridden. Any required static function can be obtained from its declaring library, and there is no need to bring it into scope via inheritance. Experience shows that developers are confused by the idea of inherited methods that are not instance methods.

Of course, the entire notion of static methods is debatable, but it is retained here because so many programmers are familiar with it. Dart static methods may be seen as functions of the enclosing library.

like image 109
Alexandre Ardhuin Avatar answered Sep 22 '22 16:09

Alexandre Ardhuin