Closed
Description
When the iterable in a for-in loop needs inference the variable doesn't get the right inferred type. Examples shown with implicit-casts: false
, but the broken inference can cause problems in other ways without that option - such as no good feedback in the IDE
An example:
Broken:
void doStuff(int x) {}
void main() {
var ints = [1];
for (var x in ints.map((i) => i)) {
doStuff(x); // The argument type 'dynamic' can't be assigned to the parameter type 'int'.
}
}
Working if you add a variable so the type is already inferred:
void doStuff(int x) {}
void main() {
var ints = [1];
var intermediate = ints.map((i) => i);
for (var x in intermediate) {
doStuff(x); // No static error
}
}
Also works if you force the type of the loop variable
void doStuff(int x) {}
void main() {
var ints = [1];
for (int x in ints.map((i) => i)) { // No static error
doStuff(x);
}
}
cc @munificent