-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Type inference of function return value #33137
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
This is by design. We do infer return types of non-recursive local functions (functions declared inside of the scope of another function or method), but for top level functions and methods, we do not infer return types (except via override inference). The reasons are as follows:
For primarily these reasons, we do not infer return types for top level functions and methods. Leaving off the return type is just another way of saying |
@leafpetersen apologies to dig up such an old issue as wasn't sure if this warranted a new issue, but would those reasons for not inferring method types still apply to something like:
this recently cuaght me out as I expected the analyzer to infer the return type of |
Well, we could certainly do return type inference for non-recursive functions, if that's what you mean. It's a bit finicky to say what we mean by "recursive" though. You make the definition syntactic (compute the connected components by free variables), or more semantic ("do I need to reference the type of I think to make that have non-surprising behavior, we'd probably want to make it an error if inference couldn't be done. For example, suppose we chose the first definition of recursive. Then this code: g(int x) {
return 3;
}
f(int x) {
g(x);
return g(x);
} happily infers g(int x) {
if (x > 0) f(x -1);
return 3;
} then suddenly the two types form a connected component, and are hence considered mutually recursive for inference. If we didn't make that an error, then adding such a call to So if we made such a change, we would probably want to make it an error if any functions in a connected component did not declare explicit return types. This seems reasonable to me on brief thought, but is a significant breaking change. The usage profile of Dart users seems to be continuing to skew towards a preference for static typing + inference (over its more dynamic origins), so at some point I do think it would be good for us to consider making inference a bit stronger. It would need to be managed as an opt-in breaking language version release though.
Not sure what you mean by this - do you mean that the type of the parameter to |
@leafpetersen Thanks for the really excellent and detailed explaination! Also I'd agree with your view of the Dart users, I'm definitely one of those new comers to Dart thru Flutter, having a Java and more recently Kotlin background and anecdotally in our local Flutter usergroup that seems to be very common and several of us have discussed being surprised by this sort of thing when coming from "stricter" world of Kotlin. I actually came across this issue because I only discovered now, after several months of Flutter usage the availability of https://dart.dev/guides/language/analysis-options#enabling-additional-type-checks and specifically |
Here's the same example as above using arrow functions: T seq<T>(Object x, T y) => y;
g(int x) => 3;
f(int x) => g(x); Where changing the definition of
Good to know, thanks.
@srawlins is doing some work to provide stronger opt in checking. The upcoming NNBD release will also take some big steps that way. Beyond that, it's hard to say, but we certainly appreciate, and listen to, the feedback. |
I think that analyzer:
language:
strict-inference: true |
Considering a simple function like this:
VSCode(with Dart extension) tells me the return type of
add
function isdynamic
:But if we add a wrong type, like
Then it shows an error:
Obviously the compiler knows the return value is a
int
, but if we don't declare it explicitly, it will be adynamic
. Is it by design?The text was updated successfully, but these errors were encountered: