-
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
Only partial passing of generic types #3119
Comments
With this particular example I'd actually argue that In short, you're specifying the typing information at a crucial point in the code (where it's actually helping a reader of the code). This typing information then flows in useful ways to all other parts of the invocation. So maybe the fix is to stop considering that approach as a workaround? ;-) There is a more general issue here, though: We might want to use a type parameter which has a non-trivial upper bound, because this allows us to preserve information. In the following example, we use Iter foo<Iter extends Iterable<Elm>, Elm>(Iter iter, Elm elm) {
return switch (iter) {
List<Elm>() => iter..add(elm),
_ => iter,
};
}
void main() {
var xs = [1];
var ys = foo(xs, 2); // `ys` has type `List<int>`, not just `Iterable<int>`.
} We have considered using type patterns (#170) to provide a feature which is intended to do this kind of work. It basically relies on the ability to introduce type parameters by a pattern matching process rather than receiving them as actual type arguments from the call site. We would then do this: Iter foo<Iter extends Iterable<final Elm>>(Iter iter, Elm elm) {
return switch (iter) {
List<Elm>() => iter..add(elm),
_ => iter,
};
}
void main() {
var xs = foo([1.5], 2); // `xs` has type `List<double>`, `2` means `2.0`.
} In |
thanks a lot @eernstg you are right, I never had a problem with this but it seems users of my package seem to struggle if they don't pass the type as a generic parameter. |
OK, I had problems how to phrase this issue correctly. I'm currently working on
watchIt
the successor for the get_it_mixin and hit again a limitation of current Dart with generic types. In this case the function needs two generic types but one of them can be guessed by the analyzer without problems but I still have to pass both.If we take this function here and I want to observe a propety
name
of aChangeNotifier
called Model.no I have to write
however it is possible to write
In that case both generic types can be guessed correctly by the analyser. But it feels like a bit of a workaround because I want to pass the first type to tell the function which type it should access and then deduct from the selector function the type of the other parameter. So that you could type
not really clear how that could be solved syntactically perhaps like:
The text was updated successfully, but these errors were encountered: