-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Can strong mode inference for Future.then be improved? #25944
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
I'm running into this and having to do some ugly workarounds: Future<Foo> getFoo(String key) {
return doSomething(). then((FooCache cache) {
return cache.get(key); // FooCache#get returns a Future<Foo>
});
} has to be rewritten as: Future<Foo> getFoo(String key) {
return doSomething(). then((FooCache cache) {
return cache.get(key); // FooCache#get returns a Future<Foo>
}).then((x) => x);
} |
Can you rewrite that using async/await? Another workaround might be: Future<Foo> getFoo(String key) {
var result = doSomething(). then((FooCache cache) {
return cache.get(key); // FooCache#get returns a Future<Foo>
});
return result;
} |
Thanks, I was able to just use async/await |
👍 , I think for import 'dart:async';
Future<int> a() => new Future<int>.value(2);
Future<int> b() => a().then((int i) => i);
int c(int i) => i;
Future<int> d() => a().then(c);
// Why strong mode errors on lines 9 (p) and 11 (r)?
Future<int> p() => a().then((a) => new Future<int>.value(2));
Future<int> q(int i) => new Future<int>.value(i);
Future<int> r() => a().then(q);
// If we help it out with a comment, no more warnings.
Future<int> x() => a().then/*<Future<int>>*/((a) => new Future<int>.value(2));
Future<int> y(int i) => new Future<int>.value(i);
Future<int> z() => a().then/*<Future<int>>*/(y); I expect |
Without union (or intersection) types, we can't really express the type of Future.then. The current state is a poor compromise that makes inference work for things like your a, b, c, and d, at the expense of making inference fall down on your p and r. At some point soon, I will implement ad-hoc inference for Future.then (basically pretending that we have union/intersection types for this specific case). Until then, we're stuck with the workarounds. Where possible, I believe that using async/await is a better solution: Future<int> p() async {
var aresult = await a();
var result = await (new Future<int>.value(2));
return result;
}
Future<int> r() async {
var ar = await a();
return await q(ar);
} Another option is to write helpers to untangle the overloading: Future/*<S>*/ thenV/*<T, S>*/(Future/*<T>*/ f, /*=S*/callback(/*=T*/ x)) => f.then/*<S>*/(callback);
Future/*<S>*/ thenF/*<T, S>*/(Future/*<T>*/ f, /*=Future<S>*/callback(/*=T*/ x)) => f.then/*<Future<S>>*/(callback); This will allow you to write your examples above as follows: Future<int> a() => new Future<int>.value(2);
Future<int> b() => thenV(a(), ((int i) => i));
int c(int i) => i;
Future<int> d() => thenV(a(), c);
Future<int> p() => thenF(a(), (a) => new Future<int>.value(2));
Future<int> q(int i) => new Future<int>.value(i);
Future<int> r() => thenF(a(), q); |
Thanks much for the tips! |
I can take a look |
Also related to #25322, which affects |
Well the good news: I've got a prototype with all of these cases working better, by introducing a The bad news: we've got some tough choices to make before it can be checked in. Having special types that exist for inference, but do not exist elsewhere, is hard to get right. The prototype does not have the boundary fully working yet. My attempts at improving the boundary has lead to other problems, like ErrorVerifier getting confused and either dropping errors/warnings or issuing spurious ones. There are related questions like how to explain what is going on with Future.then/async/await in error messages. For example, an invalid return type is normally handled via the message: (One case where inference is not as good is this mixture of Future.then and Iterable.fold, but the root cause there is the lack of unified upwards+downwards inference.) |
https://codereview.chromium.org/2208953002/ has made some progress on this. |
Wooo! Thanks @jmesserly ! |
Noting these down for future consideration.
The text was updated successfully, but these errors were encountered: