-
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
Static extension methods: implement abstract class #475
Comments
No, this will not be possible with static extension methods. As such, passing a Or, in short, static extension methods are not traits. |
Thanks for the quick response 😊 |
Some ideas in this space have been under discussion for a while, e.g. here. I don't think there's any strong short term plans in that direction yet though. I'm going to close this issue, since I think it's mostly covered by the issue referenced above - feel free to comment there on how that proposal sketch would/wouldn't address your use case. |
Just presenting a use case, I am porting some Swift code over to Dart, and they make use of extensions to implement protocols on classes imported from other packages. Something like: class Foo {
String get foo => "foo";
}
abstract class Protocol {
String get bar;
}
extension on Foo implements Protocol {
String get bar => "bar";
} Is there a possibility this can be added in the future? Is this a complex feature to implement now that we have static extension methods? |
@rrousselGit I've noticed swift has a very large namespace by default, does that have any effect on why they are able to do implementations with extensions? It doesn't seem like it would at first glance, but your reply seems to allude that it might. |
My bad, ignore my previous comment. I've misread the code snippet. I thought it was: class Foo implements Protocol {
String get foo => "foo";
// bar not implemented
}
extension on Foo implements Protocol {
// implement bar as an extension
String get bar => "bar";
} |
Ahhh, gotcha! Yeah the question is if you can use extensions to make an imported class implement a protocol/interface of some sort. Would be a real nice way to write adapters! Another question, not sure if this is the right place for this, or if this is for Stack. import 'package:flutter/widgets.dart';
import 'package:rxdart/rxdart.dart';
main() {}
extension on StreamBuilder<T> {
static StreamBuilder<T> seeded({Key key, ValueObservable<T> stream, AsyncWidgetBuilder<T> builder}) =>
StreamBuilder<T>(key: key, stream: stream, initialData: stream.value, builder: builder);
}
class FakeWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return StreamBuilder<bool>.seeded(
stream: Observable.just(true).shareValueSeeded(true),
builder: (context, snapshot) {
///
},
);
}
} Possible to add static methods? Right now this is invalid, showing error
|
No. You'd have to refer to the extension name not the type it's on to invoke a static. As a consequence you can't call a static on an unnamed extension. I filed #617 to discuss whether we should make the definition a warning. extension Seeded on StreamBuilder {
static StreamBuilder<T> seeded<T>(...) =>;
}
// usage
return Seeded.seeded<bool>(...); |
Yeah for this specific case I can't find a way to make extensions improve over this style of implementation. If we could extend a class with a new constructor or create an extension with its own constructor or add a static method to the original class I believe it would be an improvement. class ValueObservableBuilder<T> extends StreamBuilder<T> {
ValueObservableBuilder({
Key key,
ValueObservable<T> valueObservable,
AsyncWidgetBuilder<T> builder,
}) : super(key: key, stream: valueObservable, initialData: valueObservable.value, builder: builder);
} |
just swift can do it . |
Wish Dart could support this feature like Swift. |
Will it be possible to static extension methods to implement some abstract classes (interfaces), like it possible extension to implement protocol in Swift, e.g.:
Such a feature helps a lot to extend primitive type to use for framework's purposes. For example, imagine that we develop an http framework, we can extend String, Map, etc. to provide capability to return it as response to the request, e.g.:
The text was updated successfully, but these errors were encountered: