Description
The static extension method proposal allows extension methods to be used as tear offs.
extension MyExtension on int {
int inc({int by : 1}) => this + by;
}
void main() {
var inc3 = 3.inc;
assert(inc3() == 4);
assert(inc3(by : 3) == 6);
}
An open question is whether we should require tear offs from the same receiver to compare equal? That is, do we require that assert(3.inc == 3.inc)
? This does not naturally fall out of the semantic interpretation of extension methods as curried static function calls. However, we do require that instance method tear offs from the same receiver compare equal, mostly to make registering and unregistering event handlers work. It may be surprising to users that instance method tear offs compare equal, but not extension method tear offs.
If we do require these to be equal, the question then arises what to do about generic extension methods. Consider:
extension MyExtension<T> on T {
Type getType() => T;
int three => 3;
}
void main() {
List<int> x = [3];
Object y = x;
print(x.getType == y.getType);
print(x.three == y.three);
print(x.getType == x.getType);
}
The first print statement must surely print false, since the tear offs in question close over different generic type parameters.
The second print statement could be required to print true, since the tear off does not close over the type variable of the extension. It seems odd though to expose an implementation detail ("does the method close over the type variable") in the semantics of equality.
The third print statement could be required to be true, since the tear offs close over equal type variables. It feels a bit odd to specify that we take equality of closed over type variables into account, but it could work. Do we then require dis-equality of the second print statement?
cc @Hixie Does flutter rely on equality of tear offs in their API?
cc @ferhatb @matanlurey How common is it to rely on tear off equality in modern angular code?