OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of dart.core; | 5 part of dart.core; |
6 | 6 |
7 /** | 7 /** |
8 * An indexable collection of objects with a length. | 8 * An indexable collection of objects with a length. |
9 * | 9 * |
10 * Subclasses of this class implement different kinds of lists. | 10 * Subclasses of this class implement different kinds of lists. |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 * growableList.length = 3; | 70 * growableList.length = 3; |
71 * | 71 * |
72 * To create a growable list with a given length, just assign the length | 72 * To create a growable list with a given length, just assign the length |
73 * right after creation: | 73 * right after creation: |
74 * | 74 * |
75 * List growableList = new List()..length = 500; | 75 * List growableList = new List()..length = 500; |
76 * | 76 * |
77 * The [length] must not be negative or null, if it is provided. | 77 * The [length] must not be negative or null, if it is provided. |
78 */ | 78 */ |
79 factory List([int length]) { | 79 factory List([int length]) { |
| 80 dynamic list; |
80 if (length == null) { | 81 if (length == null) { |
81 return JS('', '[]'); | 82 list = JS('', '[]'); |
| 83 } else { |
| 84 // Explicit type test is necessary to guard against JavaScript conversions |
| 85 // in unchecked mode. |
| 86 if ((length is !int) || (length < 0)) { |
| 87 throw new ArgumentError("Length must be a non-negative integer: $length"
); |
| 88 } |
| 89 list = JS('', 'new Array(#)', length); |
| 90 // TODO(jmesserly): consider a fixed array subclass instead. |
| 91 JS('void', r'#.fixed$length = Array', list); |
82 } | 92 } |
83 // Explicit type test is necessary to guard against JavaScript conversions | 93 // TODO(jmesserly): skip this when E is dynamic and Object. |
84 // in unchecked mode. | 94 JS('void', 'dart.setType(#, List\$(#))', list, E); |
85 if ((length is !int) || (length < 0)) { | 95 // TODO(jmesserly): compiler creates a bogus type check here. |
86 throw new ArgumentError("Length must be a non-negative integer: $length"); | |
87 } | |
88 var list = JS('', 'new Array(#)', length); | |
89 // TODO(jmesserly): consider a fixed array subclass instead. | |
90 JS('void', r'#.fixed$length = Array', list); | |
91 return list; | 96 return list; |
92 } | 97 } |
93 | 98 |
94 /** | 99 /** |
95 * Creates a fixed-length list of the given length, and initializes the | 100 * Creates a fixed-length list of the given length, and initializes the |
96 * value at each position with [fill]: | 101 * value at each position with [fill]: |
97 * | 102 * |
98 * new List<int>.filled(3, 0); // [0, 0, 0] | 103 * new List<int>.filled(3, 0); // [0, 0, 0] |
99 * | 104 * |
100 * The [length] must not be negative or null. | 105 * The [length] must not be negative or null. |
101 */ | 106 */ |
102 factory List.filled(int length, E fill) { | 107 factory List.filled(int length, E fill) { |
103 List result = new JSArray<E>.fixed(length); | 108 List result = new List<E>(length); |
104 if (length != 0 && fill != null) { | 109 if (length != 0 && fill != null) { |
105 for (int i = 0; i < result.length; i++) { | 110 for (int i = 0; i < result.length; i++) { |
106 result[i] = fill; | 111 result[i] = fill; |
107 } | 112 } |
108 } | 113 } |
109 return result; | 114 return result; |
110 } | 115 } |
111 | 116 |
112 /** | 117 /** |
113 * Creates a list containing all [elements]. | 118 * Creates a list containing all [elements]. |
(...skipping 627 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
741 * | 746 * |
742 * List<String> words = ['fee', 'fi', 'fo', 'fum']; | 747 * List<String> words = ['fee', 'fi', 'fo', 'fum']; |
743 * Map<int, String> map = words.asMap(); | 748 * Map<int, String> map = words.asMap(); |
744 * map[0] + map[1]; // 'feefi'; | 749 * map[0] + map[1]; // 'feefi'; |
745 * map.keys.toList(); // [0, 1, 2, 3] | 750 * map.keys.toList(); // [0, 1, 2, 3] |
746 */ | 751 */ |
747 Map<int, E> asMap() { | 752 Map<int, E> asMap() { |
748 return new IterableMixinWorkaround<E>().asMapList(this); | 753 return new IterableMixinWorkaround<E>().asMapList(this); |
749 } | 754 } |
750 } | 755 } |
OLD | NEW |