-
Notifications
You must be signed in to change notification settings - Fork 1.7k
List.toList() Type problem #55321
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
Because your code is not type safe. And nor is It's the same error you'll get from List<Object?> list = <C>[];
list.add(B()); // Type error, B is not a C The function If you try to add a If you do If you do |
Thanks for answer but I still don't get it. |
If Doing Which means: List<Object> list = <int>[1]; // Valid, up-cast.
var list2 = list.toList(); // Same as `List<int>.of(list);`, called by list itself, which knows it's an `int` list.
var list3 = List.of(list); // Same as `List<Object>.of(list);`, inferred from static type. Those two calls are not the same. |
So is that an expected behaviour? Why doesn't an analyzer warn about it or something? |
It's expected and specified behavior. The analyzer doesn't warn, because it's perfectly valid code. The inherent unsoundness in covariant generics is so deep in the language that it's impossible to warn about it, because that would make almost all uses of (for example, but not limited to) It might be possible to locally deduce that you are casting a |
This tracker is for issues related to:
dart:async
,dart:io
, etc.)Some other pieces of the Dart ecosystem are maintained elsewhere.
Please file issues in their repository:
Let's assume I have such code:
So if I use
toList()
I will getTypeError
:But
List.of()
doesn't produce that error:I wonder why?
toList()
under the hood usesList.of()
but the result is different.List.from
and[...contents]
cause no problem, onlytoList()
does.Also that will work:
The text was updated successfully, but these errors were encountered: