Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1428)

Unified Diff: sdk/lib/collection/linked_hash_set.dart

Issue 838463002: Change List/Set/Map/Queue.from constructrs to accept any iterable. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: sdk/lib/collection/linked_hash_set.dart
diff --git a/sdk/lib/collection/linked_hash_set.dart b/sdk/lib/collection/linked_hash_set.dart
index c96d94db571720155511ac3a7bbf228603b5da95..af13525c8b5d03a824c4d3e9dc7cfdebef0d2176 100644
--- a/sdk/lib/collection/linked_hash_set.dart
+++ b/sdk/lib/collection/linked_hash_set.dart
@@ -65,8 +65,26 @@ abstract class LinkedHashSet<E> implements HashSet<E> {
*/
external factory LinkedHashSet.identity();
- factory LinkedHashSet.from(Iterable<E> iterable) {
- return new LinkedHashSet<E>()..addAll(iterable);
+ /**
+ * Create a linked hash set containing all of [elements].
+ *
+ * Creates a linked hash set as by `new LinkedHashSet<E>()` and adds each
+ * element of`elements` to this set in the order they are iterated.
+ *
+ * All the [elements] should be assignable to [E].
+ * The `elements` iterable itself may have any element type,
+ * so this constructor can be used to down-cast a `Set`, for example as:
+ *
+ * Set<SuperType> superSet = ...;
+ * Iterable<SuperType> tmp = superSet.where((e) => e is SubType);
+ * Set<SubType> subSet = new LinkedHashSet<SubType>.from(tmp);
+ */
+ factory LinkedHashSet.from(Iterable<E> elements) {
+ LinkedHashSet<E> result = new LinkedHashSet<E>();
+ for (final E element in elements) {
Søren Gjesse 2015/01/05 15:35:15 Any particular reason for using final here?
Lasse Reichstein Nielsen 2015/01/06 10:14:45 No. Copied it from somewhere else that had a final
+ result.add(element);
+ }
+ return result;
}
/**

Powered by Google App Engine
This is Rietveld 408576698