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
I'd like to say that QueueList's cast and retype do not adhere to the contract laid out by List.cast and List.retype but that may be up for debate. In any case, List.cast's documentation says:
Returns a view of this list as a list of R instances, if necessary.
List.retype's documentation says, similarly:
Returns a view of this list as a list of R instances.
(Ignoring for now what "if necessary" must mean...) I believe it is intended that the return value be the same list (just a different view of it), not a new list. For example, let's play with List:
voidmain() {
var a =newList<num>()..addAll([1,2,3]);
var b = a.retype<int>();
b.add(4); // add 4 to the view.
a.add(5); // add 5 to the original.
b.remove(3); // remove 3 from the view.print('a: $a'); //=> [1, 2, 4, 5]print('b: $b'); //=> [1, 2, 4, 5]
}
They are the same list. Now let's do the same for QueueList:
import'package:collection/collection.dart';
voidmain() {
var c =newQueueList<num>()..addAll([1,2,3]);
var d = c.retype<int>();
d.add(4);
print('c: $c'); //=> {1, 2, 3} // Not the same list!print('d: $d'); //=> {1, 2, 3, null, null, null, null, null, 4} // Duck?
c.add(5);
print('c: $c'); //=> {1, 2, 3, 5}print('d: $d'); //=> {1, 2, 3, null, null, null, null, null, 4}
}
I have not checked the other cast and retype implementations yet, but when I do, I presume I'll just add on to this bug, rather than filing a bug for each class.
The text was updated successfully, but these errors were encountered:
I believe it is intended that the return value be the same list (just a different view of it), not a new list.
I don't think so (@leafpetersen@lrhn), even though I wish that was the case - I think .casttries to re-use the same list, and basically invokes .retype if not possible, making this OK.
I.e. this is just a convenience for calling List.castFrom:
Unfortunately there is no way in Dart to really "re-type" something, so unlike Dart 1's as List<String>, list.cast<String>() may return a new list if necessary to fulfill that cast.
lrhn
changed the title
ListQueue's cast and retype do not adhere to the List methods' documentation
QueueList's cast and retype do not adhere to the List methods' documentation
Mar 16, 2018
Both cast or retypeare really intended to return a view of the original collection, one that also forwards mutating methods. The "if necessary" means that cast returns the original list if it already implements the desired interface, possibly by implementing a subtype, where retype always wraps so the return type is exact. It should not create a new collection that isn't linked to the original.
The QueueList class has the extra issue of needing to implement both List.cast and Queue.cast, so it needs to return something that is both a List and a Queue. I have a class ready for that in the private parts of the SDK, because one of the plans for Dart 2 was to make ListQueue implement List as well, but that doesn't help this package.
I'd like to say that QueueList's
cast
andretype
do not adhere to the contract laid out byList.cast
andList.retype
but that may be up for debate. In any case,List.cast
's documentation says:List.retype
's documentation says, similarly:(Ignoring for now what "if necessary" must mean...) I believe it is intended that the return value be the same list (just a different view of it), not a new list. For example, let's play with
List
:They are the same list. Now let's do the same for QueueList:
I have not checked the other
cast
andretype
implementations yet, but when I do, I presume I'll just add on to this bug, rather than filing a bug for each class.The text was updated successfully, but these errors were encountered: