How can I inherit static methods in dart/flutter?
817
No you cannot do that. This excerpt is from the official Dart language specification:
Author by
Paul
Updated on January 04, 2023Comments
-
Paul over 1 year
Is it possible in Dart/Flutter to inherit static methods or factories? Or do I need to workaround this by creating an instance to access that static method?
My case is that I want to serialize an object but need to access a general parse function for them.
abstract class Foo { static Foo parse(); //Error, must have a body Foo parse();//No error but need to call Foo().parse(); by creating an instance. }
I want to create by using json so is bad practice and against performance to create a new instance to return another one?
class InheritedFoo { final String string; InheritedFoo(this.string); @override Foo parse() { return InheritedFoo("some string"); } }
Is it maybe possible to use a singleton to save performance (call InheritedFoo.inst.parse() )?
-
jamesdlin about 2 yearsIt is not possible.
static
members are not part of the class interface.static
methods are just a scope-restricted form of global functions. Your workaroundFoo().parse()
would not be much better since it would not be taking advantage of polymorphism anyway (except in the case thatFoo()
is afactory
constructor that could return different derived instances at runtime). -
Paul about 2 yearsOk, understandable, but can I maybe create an instance of Foo by itself and use the inherited to access that instance and call parse? factory constructor is not possible either to inherit. Or is there maybe a way to use reflections like java?
-
Luis Utrera about 2 yearsI think this is not possible with dart, unfortunately
-
jamesdlin about 2 years@Paul I don't understand what you mean. It might help if you describe what problem you're actually trying to solve. Inheritance is usually used for polymorphism, and polymorphism does not make sense without an object instance. You might be able to accomplish what you want by having the caller supply a callback that constructs the desired object. However, if you're trying to invent your own JSON parser, you really should just use
package:built_value
orpackage:json_serializable
or something similar. -
Paul about 2 yearsI want to call a function on an class without creating an instance. But every member of that superclass must override this function. (This function should return a new instance of it by json given) But currently I found no way to do this. (e.g. make
abstract static Foo parse()
and override that in Inherited Foo to be able to callInheritedFoo.parse()
-
jamesdlin about 2 yearsYou can't override functions without having instances. Let's say you had multiple classes that derived from a single base class and both overrode a
static
method. How would that possibly work? How would you know which override should be called without explicitly specifying which derived class to use? And once you have to explicitly specify the derived class, it's no different than not having overrides. Maybe what you want is to have a callback that can be assigned to different things depending on the context. -
Paul about 2 yearsCan't you use the runtimyType to get this information (theoretically)?. If you have the type you can know which method from which class you have to call? What about the
external
keyword?
-