Description
UPDATE: Feature specification is available here:
https://github.com/dart-lang/language/blob/master/accepted/2.7/static-extension-methods/feature-specification.md
Original in-line proposal:
Possible solution strategy for #40. This issue doesn't (currently) have an concrete proposal.
Scoped static extension methods are successfully used in C# and Kotlin to add extra functionality to existing classes.
The idea is that a declaration introduces a static extension method in a scope. The extension method is declared with a name (and signature) and on a type, and any member access with that name (and signature) on something with a matching static type, will call the extension method.
Example (C# syntax):
class Container { // Because all methods in C# must be inside a class.
public static String double(this String input) // `this` marks it as an extension method on String
{
return input + input;
}
}
...
String x = "42";
String y = x.double(); // "4242"
The method is a static method, all extension methods do is to provide a more practical way to invoke the method.
In Kotlin, the syntax is:
fun String String.double() {
return this + this;
}
The String.
in front of the name marks this as an extension method on String
, and the body can access the receiver as this
. Apart from these syntactic differences, the behavior is the same.
It's possible to declare multiple conflicting extension methods. Say, you add a floo
extension method to both List
and Queue
, and then I write myQueueList.floo()
. It's unclear which one to pick. In case of a conflict, the usual approach is to pick the one with the most specific receiver type (prefer the one on List
over the one on Iterable
), and if there is no most specific receiver type, it's a static error.
C# and Kotlin both allow overriding by signature, so there risk of conflict is lower than it would be in Dart, but the same reasoning can apply.
So, Dart should also have scoped extension methods. We can probably get away with a Kotlin-like syntax, for example:
String String.double() => this + this;
Extension methods can be used on any type, including function types and FutureOr
(although probably not very practically on the latter). It can also be a generic function.
Example:
T List<T>.best<T>(bool preferFirst(T one, T other)) =>
this.fold((T best, T other) => preferFirst(best, other) ? best : other);
...
List<int> l = ...;
print(l.best((a, b) => a > b));
In this case, the type argument should probably be inferred from the static type of the receiver (which it wouldn't be if the receiver was just treated like an extra argument).
Another option is to allow:
T List<var T>.best(bool preferFirst(T one, T other)) => ...
Here the method is not generic, so the type variable is bound by the list, and it will use the actual reified type argument at run-time (and the static type at compile-time, which can cause run-time errors as usual).
Example:
List<T> List<var T>.clone() => new List<T>.from(this);
Then anyList.clone()
will create another list with the same run-time element type as anyList
.
It effectively deconstructs the generic type, which nothing else in Dart currently does. The extension method gets access to the reified argument type, just as a proper member method does, which is likely to be necessary for some functionality to be implemented in a useful way.
(Type deconstruction is a kind of pattern-matching on types, it might make sense in other settings too, like if (x is List<var T>) ... use T ...
).
This feature might not be possible, but if it is, it will be awesome 😄.
One issue with extension members is that they can conflict with instance members.
If someone declares T Iterable<T>.first => this.isEmpty ? null : super.first;
as a way to avoid state errors, it should probably shadow the Iterable.first
instance getter. The decision on whether to use the extension method is based entirely on the static type of the receiver, not whether that type already has a member with the same name.
Even in the case where you have a static extension method on Iterable
for a member added in a subclass, say shuffle
on List
, an invocation of listExpression.shuffle()
should still pick the static extension method, otherwise things are too unpredictable.
Another issue is that static extension methods cannot be invoked dynamically. Since dispatch is based on the static type, and you can't put members on dynamic
(or can you? Probably too dangerous), there is nothing to match against.
Or would putting a static extension method on Object
work for dynamic
expressions?
If so, there is no way out of an extension method on Object
, so perhaps casting to dynamic would be that out.
We say that static extension methods are available if the declaration is imported and in scope. It's not clear whether it makes any difference to import the declaring library with a prefix. There is no place to put the prefix, and Dart does not have a way to open name-spaces, which would otherwise make sense.
If we get non-nullable types, it might matter whether you declare int String.foo() => ...
or int String?.foo() => ...
. The latter would allow this
to be null
, the former would throw a NSMError instead of calling when the receiver is null
(and not a String
). Until such time, we would have to pick one of the behaviors, likely throwing on null
because it's the safest default.
We should support all kinds of instance members (so also getters/setters/operators). The syntax allows this:
int get String.count => this.runes.length;
void set String.count(int _) => throw 'Not really";
int String.operator<(String other) => this.compareTo(other);
We cannot define fields because the method is static, it doesn't live on the object.
We could allow field declarations to be shorthand for introducing an Expando
, but expandos don't work on all types (not on String
, int
, double
, bool
, or null
, because it would be a memory leak - mainly for String
, int
and double
- the value never becomes unavailable because it can be recreated with the same identity by a literal).
Metadata
Metadata
Assignees
Type
Projects
Status