-
Notifications
You must be signed in to change notification settings - Fork 213
Infering type argument inside list #3567
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
The ability to obtain If you want to know the details then instantiation to bound is specified here, which is the section 'The instantiation to bound algorithm' in the PDF). This algorithm will find a value for all the type arguments of the given expression. This is used, e.g., if you declare a variable to have type In this context, the crucial point is that instantiation to bound is done at the very end. Type inference proceeds by propagating information downward into the given expression (in order to choose actual type arguments matching the expectations of the context) and then performs type inference bottom-up based on the given expressions, and type parameters that haven't yet gotten any constraints will be chosen by instantiation to bound. In the given example there are no constraints on We have a request for passing additional information about type parameter bounds here. I think this issue is basically a case that serves as an argument in favor of pushing on that idea: If we pass down the constraint that @leafpetersen, @natebosch, @stereotype441, do you have further comments? |
This keeps coming up (as the linked issues show, good sleuthing there), so it's probably worth trying to address. I think we're shooting ourselves in the foot a little, by using To include the bound, we'd have to infer That means that we can safely infer So if we could we give Take: T id<T>(T value) => value;
void foo<K extends A, V extends B>(Map<K, V> _) {}
foo({"A": id(42)); where the context type of Then Which means I've just (again) introduced context type hints, a context type that is a bound, but is not a requirement. With that, maybe we can even use |
What do you mean? Something like #1674? I could not follow. |
I'm considering cases where the context type contains the same type parameter more than once. Something like: (T, T) maybeSwap<T>(bool swap, (T, T) pair) {
if (swap) return (pair.$2, pair.$1);
return pair;
}
(S, T) createPair<S, T>(..) {...}
var v = swap(someBool, createPair(...)); Here the context type of I have no idea whether that can actually make a difference. Probably not in an immediate function call like this, but maybe if it's a more complicated inlined expression. Or not. |
I don't think this has much to do directly with the matching and constraint solving part of inference. I think if you wanted to make progress with this you would want to look at the definitions of greatest and least closure, which is where I think you would use this information. |
I filed an issue with a description of the changes to the meta-theory that I believe would be required to improves this here. |
If you paste the following code into dartpad.dev:
I get the following errors for line 7
const a = A([]);
:I'm not sure this is working as intended, but for me as a user, this is a little weird, since the type
T
as I understand has a lower-bound type attached to it (num
), so why couldn't theList<T>
be inferred toList<num>
?For example, when I do:
I get no errors.
The text was updated successfully, but these errors were encountered: