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?
Say I have the abstract class A
abstract class A {
A.someConstructor(Foo foo);
}
and all subclasses of A should then implement such constructor:
class B extends A {
#override
B.someConstructor(Foo foo) {
// ...
}
}
So basically what I want is some kind of abstract constructors.
Is there any way of achieving this (of course the above code does not work) or do I need a normal abstract method which then creates the object and sets its properties?
EDIT: Ok so it looks like the only way to create at least a similar behaviour would be something like this:
abstract class A {
A.someConstructor(Object foo);
}
class B extends A {
B.someConstructor(Object foo) : super.someConstructor(foo) {
// ...
}
}
This isn't exactly useful, and after some thinking about my problem I realized that in fact my original goal itself is not really neccessary, so this questions is now answered.
You want to enforce a pattern on the constructors of subclasses. The Dart language has no support for doing that.
Dart has types and interfaces which can be used to restrict values and class instance members.
If a class implements an interface, then its instance members must satisfy the signatures declared by the super-interface. This restricts instance members.
If a variable has a type, for example a function type, then you can only assign values of that type to it. This restricts values. Because a class is a subtype of its interfaces, the subclass restriction means that class typed variables can be used safely (the subtype can be used as its supertype because it has a compatible interface).
There is no way to restrict static members or constructors of classes, or members of libraries, because there is no way to abstract over them. You always have to refer directly to them by their precise name, so there is no need for them to match a particular pattern.
(Which may explain why you found the goal not necessary too).
In this situation, your subclasses must call the A.someConstructor constructor, but they are free to choose the signature of their own constructors. They can do:
class B extends A {
B.someConstructor(Object foo) : super.someConstructor(foo);
}
// or
class C extends A {
C.differentName(Object foo) : super.someConstructor(foo);
}
// or even
class D extends A {
D() : super.someConstructor(new Object());
}
Constructors aren’t inherited
Subclasses don’t inherit constructors from their superclass. A
subclass that declares no constructors has only the default (no
argument, no name) constructor.
Source
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.