You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Request to make extension methods behave like actual class member functions. Here is an example, calling type casting Center widget and calling the extension method calls Widget extension method instead of Center extension method. This will allow developers to add functionality to the library or framework without actually modifying the library's source code.
`
import 'package:flutter/material.dart';
Here's an existing proposal which would introduce support for extension methods with late binding: #177. It also illustrates why this is a non-trivial undertaking.
We didn't go in that direction when extension declarations were added to the language, and it would be a radical change of the extension member mechanism to do it today. So it's not likely that we will a feature like #177 today.
However, you can of course implement the dispatch yourself:
// A bit of glue code, to make this example runnable on its own.classColors {
staticconstdynamic yellow =1;
}
classWidget {}
classCenterimplementsWidget {
Widget? child;
Center({this.child});
}
classContainerimplementsWidget {
Widget? child;
Container({this.child, width, height, color});
}
voidrunApp(_) {}
// Example code, adjusted to have a dispatcher.voidmain() {
Widget widget =Center(
child:Container(
width:50,
height:50,
color:Colors.yellow,
),
);
widget.foo();
runApp(widget);
}
extensionWidgetExtensiononWidget {
voidfoo() {
final self =this;
switch (self) {
caseContainer():
self.containerFoo();
caseCenter():
self.centerFoo();
caseWidget():
self.widgetFoo();
/*the default is no-op*/
}
}
voidwidgetFoo() {
print("Widget.foo");
}
}
extensionCenterWidgetExtensiononCenter {
voidfoo() =>centerFoo();
voidcenterFoo() {
print("Center.foo");
child?.foo();
}
}
extensionContainerWidgetExtensiononContainer {
voidfoo() =>containerFoo();
voidcontainerFoo() {
print("Container.foo");
child?.foo();
}
}
The idea is that foo is a dispatcher method: It selects the correct implementation of "fooing" and then calls it using the static type which is obtained by promoting self.
You can write specialized versions of the dispatcher method, like CenterWidgetExtension.foo. They can be faster, because they have fewer possible types to resolve. In particular, a foo in a leaf class can just call directly. You can also ignore these specialized dispatcher methods and rely on Widget.foo in all cases.
The only remaining part is to write the implementations (centerFoo and the like). They have names that reflect their type-specific nature: We call somethingFoo when we know that this is the exact implementation we wish to run with the given receiver.
That said, I'll close this issue because the proposal is already covered by the material in #177.
Request to make extension methods behave like actual class member functions. Here is an example, calling type casting Center widget and calling the extension method calls Widget extension method instead of Center extension method. This will allow developers to add functionality to the library or framework without actually modifying the library's source code.
`
import 'package:flutter/material.dart';
`
The text was updated successfully, but these errors were encountered: