-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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 in for-each loop is inconsistent with behavior elsewhere #54706
Comments
This is not unique to Iterable<num> i4 = xs ?? []; // compile-time error. This issue requires the expression to have a context type, which is applied as context type for the second operand. Otherwise it would use the type of the first operand, made non-nullable, as the context type of And then the least upper bound of It's operating the way it is for a reason, which mostly, or at least often, works, and it could have worked (and given |
i don't understand. why would the context for assignment to |
The Dart performs type inference with an optional context type, if it can see that the context expects a specific type, or an empty context if it can't see any requirements. The inference of The assignment to For So, Then we hit the problem that least-upper-bound of |
One other way of solving the compile-time error would be: for (final x in [...?xs]) {} // no problem |
the following code
yields a compiler error on the
xs ?? []
loop, but not theys
loop.this is counterintuitive and annoying. one would think that whatever type the compiler inferred
ys
isit should also infer
xs ?? []
is in its for loop since it's the same expression.notice that the
zs ?? []
loop does not have a compiler error.the difference here is that
zs
is aList
, whilexs
is anIterable
.i feel like this shouldn't matter, but somehow it does.
why should putting
xs ?? []
into a variable and then using that variableyield different behavior than simply inlining it?
a workaround is to put
xs ?? <int>[]
into the loop, but we only need this seeminglysuperfluous type annotation when
xs
is anIterable
rather than aList
,and for some reason, we don't need it when declaring
ys
.is this a bug relative to spec, or some unintended consequence of the spec?
The text was updated successfully, but these errors were encountered: