39

In an abstract class, I wish to define static methods, but I'm having problems.

In this simple example

abstract class Main { static String get name; bool use( Element el ); } class Sub extends Main { static String get name => 'testme'; bool use( Element el ) => (el is Element); } 

I receive the error:

function body expected for method 'get:name' static String get name;

Is there a typo in the declaration, or are static methods incompatible with abstract classes?

3 Answers 3

41

Dart doesn't inherit static methods to derived classes. So it makes no sense to create abstract static methods (without implementation).

If you want a static method in class Main you have to fully define it there and always call it like Main.name

== EDIT ==

I'm sure I read or heard some arguments from Gilad Bracha about it but can't find it now.

This behaviour is IMHO common mostly in statically typed languages (I don't know many dynamic languages). A static method is like a top level function where the class name just acts as a namespace. A static method has nothing to do with an instantiated object so inheritance is not applicable. In languages where static methods are 'inherited' this is just syntactic sugar. Dart likes to be more explicit here and to avoid confusion between instance methods and static methods (which actually are not methods but just functions because they don't act on an instance). This is not my primary domain, but hopefully may make some sense anyways ;-)

Sign up to request clarification or add additional context in comments.

3 Comments

do you know if there a rationale for not to inheriting static methods?
Extended the answer because the text size exceeds max comment length.
@ccyoung My guess is that it's more confusing than beneficial. One can emulate static inheritance by using reflection, i.e. by calling a super-type's implementation or calling a static method by name for getting 'overridden' behavior. Would be interesting to know what's the real answer is.
3

Looks like you are trying to 'override' a static method. I'm not sure what you are trying to achieve there. I'm not aware of any OO languages that support that (and not sure how they could).

A similar question in Java might help clarify Polymorphism and Static Methods

Note also that it is considered bad practice to refer to statics from an instance of the class in Java (and other OO languages). Interestingly I noticed Dart does not let you do this so is in effect removing this bad practice entirely.

So you couldn't even fool yourself into thinking it would behave polymorphically in Dart because you can't call the static from the instance.

7 Comments

Swift has Type Methods with the keyword class, which is similar to static, but you can override it.
Yes, afaik Swift is the only language that supports it (and many other revolutionary things). This request is open to support this in Dart: github.com/dart-lang/language/issues/356.
Objective-C as well :)
And Python's @classmethods
That feature would be useful for an ORM framework. It would allow creating something like abstract static String tableName, then a concrete class can @override static String tableName = 'users' and do something like query = 'SELECT * FROM $tableName', without having to repeat the tableName string or creating a regular final field in memory for each instance just to fulfill this behavior.
|
0

I was wondering the same thing. I ended up taking the desired function as a required parameter in the constructor of the abstract base class. E.g., (simplified/untested ~pseudocode):

abstract class AbstractBase { final String Function() _instanceFGetName; void sayHello() { print("Hello there, ${_instanceFGetName()}"); } AbstractBase(this._instanceFGetName); } class Sub extends AbstractBase { Sub._(super._instanceFGetName); static String _staticFGetName() => 'testme'; factory Sub() { return Sub._(_staticFGetName); } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.