-
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
Type inference compilation error on callable class #3122
Comments
There is no information flow that will inform the choice of formal parameter types of a function literal based on code in the body of the function literal. In other words, the type of In this case the context type is The value of Hence, passing I think the main thing to emphasize here is that there is no way to transfer information from the body of a function literal to the types of formal parameters, they get everything (if anything) from the context type of the function as a whole. So in the case where the formal parameter types aren't inferred as you would expect, please double check that there is some useful information in the context type. I'll close the issue because it is all working as intended. |
Sorry @eernstg but based in the informations you given I still don't understand why with this little change: - fn: a,
+ fn: a.call, everything works as expected without any compilation error. Isn't |
Sorry, I missed that question. The 'callable class' feature is a residue after a more general mechanism in Dart 1: An instance of a class that declares a method named However, this feature doesn't come for free. In particular, if every function invocation is really an implicit invocation of the So we removed this very general mechanism and replaced it by a very shallow compile-time mechanism: If an instance of a statically known class that defines a method named So if This won't happen if you use the expression However, That's probably the reason why Are you sure you actually need to use a callable class in this situation? Couldn't it simply be a function? I think it's a good habit to rely as little as possible on this particular mechanism (that is, Cf. #2399. |
By the way, you might want to use a more strict static analysis:
based on the following 'analysis_options.yaml': analyzer:
language:
strict-raw-types: true
strict-inference: true
strict-casts: true
linter:
rules:
- avoid_dynamic_calls |
Code giving error visible at https://dartpad.dev/dfd112dd6e0be277c2d8f3c3f7c6577f
I put the code also here for an easy reference:
This code throws a compilation error like this:
The interesting thing is that just by changing
fn: a
intofn: a.call
the error disappears and the code works as expected.Also this
fn: () => a()
works.Seems like the Type inference doesn't work correctly on callable functions when the function name is not explicitly set.
At the time of creating this issue these are the Dart and Flutter versions being used:
The text was updated successfully, but these errors were encountered: