Ryan Gonzalez
2018-04-03 00:26:34 UTC
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' where
Derived2 is from file:///home/ryan/stuff/Downloads/dart-sdk/x.dart
Derived1 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>
*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).
Tested with Dart 2.0.0-dev.44.0.
--
Ryan (ã©ã€ã¢ã³)
Yoko Shimomura, ryo (supercell/EGOIST), Hiroyuki Sawano >> everyone else
https://refi64.com/
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' where
Derived2 is from file:///home/ryan/stuff/Downloads/dart-sdk/x.dart
Derived1 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>
*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).
Tested with Dart 2.0.0-dev.44.0.
--
Ryan (ã©ã€ã¢ã³)
Yoko Shimomura, ryo (supercell/EGOIST), Hiroyuki Sawano >> everyone else
https://refi64.com/
--
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+***@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/CAO41-mNOctG0tDN4VOCHR2f3KLO_%3D5JM6kSFR6j4yURxHiLSsw%40mail.gmail.com.
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+***@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/CAO41-mNOctG0tDN4VOCHR2f3KLO_%3D5JM6kSFR6j4yURxHiLSsw%40mail.gmail.com.