Open derolf opened 5 years ago
This is a good example of a case where it would have been very useful for inference to choose int
as the parameter type for the function literal, like in Kotlin and TypeScript.
However, when inference has chosen the type argument int
for the given invocation of execute
then the first parameter is type correct, and so is the second one with parameter type dynamic
(because void Function(dynamic)
is a subtype of void Function(int)
).
In order to address the immediate problem, you could use dartanalyzer --no-implicit-dynamic
, which you might prefer anyway. (Just guessing ;-)
For the broader question of why inference did not give the parameter value
the type int
, I believe this is an instance of https://github.com/dart-lang/sdk/issues/25490, which is concerned with information flow from one argument to another one during type inference.
That kind of information flow arises automatically when type inference is done using algorithm W or similar mechanisms based on unification, but type inference using unification in a language with subtyping isn't tractable, so there's a need to go into heuristics, and that hasn't been done yet for Dart.
@leafpetersen, do you have further comments on this? I added a reference to this issue on #25490, because it's a good example to keep in mind. This issue could then be closed, marking it as a duplicate of #25490.
The problem
Consider the following example:
I would expect the Dart Analyzer to fail at
value.bar
sinceT
could be inferred tonumber
. Instead, Dart does not inferT
tonumber
, but falls back todynamic
.What are other languages doing?
Equivalent example in TypeScript:
yields error
Equivalent example in Kotlin:
yields error
https://stackoverflow.com/questions/56540905/type-inference-from-parameter-type