I'm trying to create a static extension method on one of my classes (which is autogenerated, so I can't easily modify it). According to the docs, this should be possible:
Extensions can also have static fields and static helper methods.
Yet even this small example does not compile:
extension Foo on String {
static String foo() => 'foo!';
}
void main() {
print(String.foo());
}
Error: Method not found: 'String.foo'.
print(String.foo());
^^^
What am I doing wrong?
The docs mean that the extension classes themselves can have static fields and helper methods. These won't be extensions on the extended class. That is, in your example, Foo.foo() is legal but String.foo() is not.
You currently cannot create extension methods that are static. See https://github.com/dart-lang/language/issues/723.
Note that you also might see Dart extension methods referred to as "static extension methods", but "static" there means that the extensions are applied statically (i.e., based on the object's type known at compilation-time, not its runtime type).
As James mentioned, you can't use the static method directly on the extended class as of today, the current solution to your problem would be:
extension Foo on String {
String foo() => 'foo!';
}
void main() {
print('Hi'.foo());
}
Related
Having this snippet:
abstract class SuperClass {
static String id = 'id';
static String get getId {
return id;
}
}
class SubClass extends SuperClass {
static String name = 'name';
}
void main() {
print(SubClass.name);
print(SubClass.id);
}
How can i access id property?
print(SubClass.id); results in error:
"line 3 • The getter 'id' isn't defined for the type 'SubClass'."
Doesn't SubClass should inherit the id property when extended (not implemented)?
In Dart, static methods and properties are not inherited.
This is covered in section 10.7 of the Dart language spec:
10.7 Static Methods staticMethods
Static methods are functions, other than getters or setters, whose declara-
tions are immediately contained within a class declaration and that are declared
static. The static methods of a class C are those static methods declared by C.
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.
Static method declarations may conflict with other declarations (10.10).
Other References
Dartlang GitHub issue
Google Groups discussion
What's the rationale behind not inheriting static variables, in Dart?
Dart Patterns to replace static inheritance
How can I inherit static methods in dart/flutter?
I'm trying to create a static extension method on one of my classes (which is autogenerated, so I can't easily modify it). According to the docs, this should be possible:
Extensions can also have static fields and static helper methods.
Yet even this small example does not compile:
extension Foo on String {
static String foo() => 'foo!';
}
void main() {
print(String.foo());
}
Error: Method not found: 'String.foo'.
print(String.foo());
^^^
What am I doing wrong?
The docs mean that the extension classes themselves can have static fields and helper methods. These won't be extensions on the extended class. That is, in your example, Foo.foo() is legal but String.foo() is not.
You currently cannot create extension methods that are static. See https://github.com/dart-lang/language/issues/723.
Note that you also might see Dart extension methods referred to as "static extension methods", but "static" there means that the extensions are applied statically (i.e., based on the object's type known at compilation-time, not its runtime type).
As James mentioned, you can't use the static method directly on the extended class as of today, the current solution to your problem would be:
extension Foo on String {
String foo() => 'foo!';
}
void main() {
print('Hi'.foo());
}
Generic arguments are not allowed in Swift extensions, something like
public extension SequenceOf<T>{
...
}
is not possible. Using something like
public extension SequenceOf{
public func Something<T>(){
....
}
}
leads to
Extension of generic type 'SequenceOf' from a different module cannot provide public declarations
So, how does one extend SequenceOf (or similar types) with extra methods?
The problem here is not that your are extending SequenceOf but that you are doing it in a different module (i.e. a library or a framework).
Add the source file containing the extension to the project that is using it.
Could somebody please write some formal definition of keyword with in Dart?
In official Dart examples I have only found:
class TaskElement extends LIElement with Polymer, Observable {
But I still can't understand what is it exactly doing.
The with keyword indicates the use of a "mixin". See here.
A mixin refers to the ability to add the capabilities of another class or classes to your own class, without inheriting from those classes. The methods of those classes can now be called on your class, and the code within those classes will execute. Dart does not have multiple inheritance, but the use of mixins allows you to fold in other classes to achieve code reuse while avoiding the issues that multiple inheritance would cause.
I note that you have answered some questions about Java -- in Java terms, you can think of a mixin as an interface that lets you not merely specify that a given class will contain a given method, but also provide the code for that method.
You can think mixin as Interface in Java and like protocol in Swift.Here is the simple example.
mixin Human {
String name;
int age;
void about();
}
class Doctor with Human {
String specialization;
Doctor(String doctorName, int doctorAge, String specialization) {
name = doctorName;
age = doctorAge;
this.specialization = specialization;
}
void about() {
print('$name is $age years old. He is $specialization specialist.');
}
}
void main() {
Doctor doctor = Doctor("Harish Chandra", 54, 'child');
print(doctor.name);
print(doctor.age);
doctor.about();
}
Hope it help to understand.
if I want to add something to the implementation of
public static function createPopUp(parent:DisplayObject,
className:Class,
modal:Boolean = false,
childList:String = null,
moduleFactory:IFlexModuleFactory = null):IFlexDisplayObject
{
return impl.createPopUp(parent, className, modal, childList, moduleFactory);
}
do I have to put all the arguments in my function declaration or does it pick them up implicitly?
Yes - ActionScript doesn't support method overloading only overriding, in which case your method's signature must exactly match that of the overridden method.
But you are trying to override a static method which is not possible in ActionScript at all. If you want something like in the code snippet create your class not inheriting anything, put a static createPopUp method inside and let it call static createPopUp method from the class you want to decorate and call your class'es static method instead of the original one.
This impossibility to sensible inherit (or inherit at all) static methods is one of the reasons why one should try to restrain from using statics as much as possible - statics take away the power of inheritance from OO languages.