-
Notifications
You must be signed in to change notification settings - Fork 205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
override extension methods #3321
Comments
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 However, you can of course implement the dispatch yourself: // A bit of glue code, to make this example runnable on its own.
class Colors {
static const dynamic yellow = 1;
}
class Widget {}
class Center implements Widget {
Widget? child;
Center({this.child});
}
class Container implements Widget {
Widget? child;
Container({this.child, width, height, color});
}
void runApp(_) {}
// Example code, adjusted to have a dispatcher.
void main() {
Widget widget = Center(
child: Container(
width: 50,
height: 50,
color: Colors.yellow,
),
);
widget.foo();
runApp(widget);
}
extension WidgetExtension on Widget {
void foo() {
final self = this;
switch (self) {
case Container():
self.containerFoo();
case Center():
self.centerFoo();
case Widget():
self.widgetFoo();
/*the default is no-op*/
}
}
void widgetFoo() {
print("Widget.foo");
}
}
extension CenterWidgetExtension on Center {
void foo() => centerFoo();
void centerFoo() {
print("Center.foo");
child?.foo();
}
}
extension ContainerWidgetExtension on Container {
void foo() => containerFoo();
void containerFoo() {
print("Container.foo");
child?.foo();
}
} The idea is that You can write specialized versions of the dispatcher method, like The only remaining part is to write the implementations ( 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: