This is something kind of weird I've run into in my Flutter app:class Base {}class Derived1 extends Base {}class Derived2 extends Base {}void main() {// Create a List<Derived1>.var derived1List = <Derived1>[new Derived1()];// Cast to a List<Base> (note that this assignment succeeds).List<Base> baseList = derived1List.cast<Base>();// Try adding a Derived2 to the list (fails!).baseList.add(new Derived2() as Base);}The add operation gives me:Unhandled exception:type 'Derived2' is not a subtype of type 'Derived1' of 'value' whereDerived2 is from file:///home/ryan/stuff/Downloads/dart-sdk/x.dartDerived1 is from file:///home/ryan/stuff/Downloads/dart-sdk/x.dart#0 List.add (dart:core-patch/dart:core/growable_array.dart:149)#1 main (file:///home/ryan/stuff/Downloads/dart-sdk/x.dart:8:12)#2 _startIsolate.<anonymous closure> (dart:isolate-patch/dart:isolate/isolate_patch.dart:279)#3 _RawReceivePortImpl._handleMessage (dart:isolate-patch/dart:isolate/isolate_patch.dart:165)This seems rather odd, since it kind of kills the point of List.cast<Type>
void main() {// Create a List<Derived1>.var derived1List = <Derived1>[new Derived1()];// Cast to a List<Base> (note that this assignment succeeds).
List<Base> baseList = derived1List;
// Try adding a Derived2 to the list (fails!).baseList.add(new Derived2() as Base);}
*and* makes it so that you can have a generic type that isn't actually that type (since the assignment works, so according to the type system, the operation should be valid).
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/CAF8T8Lyn%3DxiBVK3jEP%3Dv23CTObDCYA8_9Lo8a1J_kaPmwkRtUw%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/162891e5bf8.2837.db5b03704c129196a4e9415e55413ce6%40gmail.com.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/CAF8T8LyS4iRprvZE%2BhE04DWYAV9eVFQZsdR4%2BRYPT%3DtQDuCJ7w%40mail.gmail.com.
By "like this", I mean something like:derived1List.mapCast<Base>()// which does:derived1List.map((c) => c as Base)Except it wouldn't give off analyzer errors about redundant casts and would be more readable.
expectListBase(List<Base> list) {}void main() {
var derived1List = <Derived1>[new Derived1()];
expectListBase(derived1List);}
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/CAO41-mMri%3DK9HEJJmEot8sO6RwketJ61R%3DGQQuVUJSik-nbcAg%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/CAF8T8LyLqh5gbOTm7SgO5N4yaFkB%3D69hiqBNHuQM5F5xVzr8oA%40mail.gmail.com.
I guess I could just use List.from as mentioned, since this whole thing was mostly just me misunderstanding what List.cast does, and I was expecting a method for this (kind of like x.toList() is a neater-ish way of writing List<T>.from(x)).
what's the difference between cast and retype?