You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The context type actually won't reach the relevant location in this case, so type inference proceeds from a blank slate.
The syntactic context e as T actually does not provide a context type for e, so we will create a List<B> and then perform the upcast to List<A> in the example at the end of this comment. So the inference applied to [1, 2, 3].map((e) => B()).toList() works as it would have worked with final items = [1, 2, 3].map((e) => B()).toList();. But a context type wouldn't even be sufficient (say, List<A> items = [1, 2, 3].map((e) => B()).toList();), because type inference of the receiver [1, 2, 3].map((e) => B()) gets the empty context type even in the case where a method invocation (here: <receiver>.toList()) has a context type.
But I agree that the cast does have an effect: The type of items is List<A> with the cast and List<B> without it. So it is somewhat misleading that the analyzer says 'unnecessary cast'.
Thanks for clarification @eernstg. Yeah, now that I actually run the code I see that it only affects the static type of items but not its runtime type. I am a bit puzzled why it solved an issue for the reporter then:
Adding a cast as List solves the problem, but then it asks to remove unnecessary cast.
Given your explanation it is is clearly "redundant cast", though it might have downstream effects on the inference.
I suspect that it didn't resolve the problem: The compile-time error was gone, because we're no more trying to add a SizedBox to a List<Text> according to the static types, but it will still do exactly that in terms of the types at run time, and then there will be a downcast failure.
You are right, cast doesn't solve the problem. It only helps with compilation. This is the error I get at runtime: type 'SizedBox' is not a subtype of type 'Text' of 'value'
Activity
mraleph commentedon Dec 7, 2020
items
is aList<Text>
because of howmap
is defined: if you givemap
a function which returnsT
you get anIterable<T>
as a result.There are few ways to fix it:
or alternatively:
That's wrong message from the analyzer though. Self contained repro:
Produces
unnecessary_cast
but clearly should not because cast influences inference.[-]Type inference fails[/-][+]Incorrect unnecessary_cast from analyzer when cast influences type inference[/+]eernstg commentedon Dec 7, 2020
The context type actually won't reach the relevant location in this case, so type inference proceeds from a blank slate.
The syntactic context
e as T
actually does not provide a context type fore
, so we will create aList<B>
and then perform the upcast toList<A>
in the example at the end of this comment. So the inference applied to[1, 2, 3].map((e) => B()).toList()
works as it would have worked withfinal items = [1, 2, 3].map((e) => B()).toList();
. But a context type wouldn't even be sufficient (say,List<A> items = [1, 2, 3].map((e) => B()).toList();
), because type inference of the receiver[1, 2, 3].map((e) => B())
gets the empty context type even in the case where a method invocation (here:<receiver>.toList()
) has a context type.But I agree that the cast does have an effect: The type of
items
isList<A>
with the cast andList<B>
without it. So it is somewhat misleading that the analyzer says 'unnecessary cast'.mraleph commentedon Dec 7, 2020
Thanks for clarification @eernstg. Yeah, now that I actually run the code I see that it only affects the static type of
items
but not its runtime type. I am a bit puzzled why it solved an issue for the reporter then:Given your explanation it is is clearly "redundant cast", though it might have downstream effects on the inference.
eernstg commentedon Dec 7, 2020
I suspect that it didn't resolve the problem: The compile-time error was gone, because we're no more trying to add a
SizedBox
to aList<Text>
according to the static types, but it will still do exactly that in terms of the types at run time, and then there will be a downcast failure.MohiuddinM commentedon Dec 7, 2020
You are right, cast doesn't solve the problem. It only helps with compilation. This is the error I get at runtime:
type 'SizedBox' is not a subtype of type 'Text' of 'value'
scheglov commentedon Dec 8, 2020
@bwilkerson @pq
type_local_variable_instead_of_casting
#58903unnecessary_cast
false positive when cast affects inferred type #52086