-
Notifications
You must be signed in to change notification settings - Fork 213
Specialize interfaces for specific generic types on a class #276
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
Comments
For the first example, generic constructors would cover it, I think: class List<E> {
List<T extends Object>([int size]);
} The extension NumericListMethods<T extends num> on List<T> {
T sum() => ...
} |
I don't know if we have a concrete proposal yet, but my understanding had been that generic constructor type arguments are a separate list from the type's generic argument list. They would go to the right of the constructor name and not to the right of the type name. If we can solve this problem with allowing the syntax you proposed that would be great.
Yeah this use case is definitely less necessary and I can't think of a time I'd need it personally - although I can imagine that someone may want the capability for instance methods instead of extension methods so they can be overridden. |
What Bob is saying here is not "generic constructors" as we normally use the term. I agree that this is a new feature,more like generically constrained constructors. Here it's a constructor for The List<T?> List<T>([int size]) : ... because it needs to return a nullable type. The "member only exists if type parameter is something" feature is more worrysome to me. If you do More problematic would be: class C<T> {
int foo() if T extends Foo => 42
}
class D<T extends Bar> extends C<T> {
String foo() => "foo";
} I guess this declaration would not be valid because it is possible that some class might implement both We would have to treat the conditional member as always there for subclassing. That's not unreasonable. I think it could work. A dynamic call should work if the list has a So the example above would be implemented as:
but an invocation of |
Prior discussion of something similar at dart-lang/sdk#32120 (comment) |
Closing in favor of #2313 which has much more detail and a concrete proposal. |
Solution for #275
Add a way to put conditions on the existence of a constructor or method depending on the specific generic type of a class. I think this would apply to the static type at the callsite.
This would let us statically prevent certain calls that would otherwise have to throw at runtime.
Separately, in the discussion for static extension methods we learned that it may be possible to write an extension that is specialized based on the generic type. This gives us a capability with extension methods that isn't possible with instance methods. Adding instance methods conditional on the specific generic type would bring this same power without needing to separate out the implementation to a extension.
Straw man:
The text was updated successfully, but these errors were encountered: