No List constructor takes Iterator<E> #17694
Labels
area-core-library
SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries.
closed-obsolete
Closed as the reported issue is no longer relevant
core-n
library-core
type-enhancement
A request for a change that isn't a bug
You cannot create a List from an Iterator<E> - construction requires an Iterable<E> which wraps an iterator getter that returns an Iterator<E>
It would be convenient if the programmer already has an iterator to use that rather than require the Iterator be wrapped in an Iteratable. For example if a function only requires an Iterator<E> without caring about what collection it came from (map, set, random generator, whatever) and internally wanted to represent that as a list while processing it.
Though to be fair it looks like List.from(...) just goes over the collection at least once, perhaps going over and allocating a second list if fixed length is requested. Ideally this cost would be hinted at in the documentation.
// Copied from list.dart
/**
* Creates a list and initializes it using the contents of [other].
*
* The [Iterator] of [other] provides the order of the objects.
*
* This constructor returns a growable list if [growable] is true;
* otherwise, it returns a fixed-length list.
*/
factory List.from(Iterable other, { bool growable: true }) {
List<E> list = new List<E>();
for (E e in other) {
list.add(e);
}
if (growable) return list;
return makeListFixedLength(list);
}
The text was updated successfully, but these errors were encountered: