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

Side by Side Diff: sdk/lib/internal/iterable.dart

Issue 2845933002: SkipIterable - ensure field is only initialized to a known int. (Closed)
Patch Set: Created 3 years, 7 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 unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, 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._internal; 5 part of dart._internal;
6 6
7 /** 7 /**
8 * Marker interface for [Iterable] subclasses that have an efficient 8 * Marker interface for [Iterable] subclasses that have an efficient
9 * [length] implementation. 9 * [length] implementation.
10 */ 10 */
(...skipping 569 matching lines...) Expand 10 before | Expand all | Expand 10 after
580 final Iterable<E> _iterable; 580 final Iterable<E> _iterable;
581 final int _skipCount; 581 final int _skipCount;
582 582
583 factory SkipIterable(Iterable<E> iterable, int count) { 583 factory SkipIterable(Iterable<E> iterable, int count) {
584 if (iterable is EfficientLengthIterable) { 584 if (iterable is EfficientLengthIterable) {
585 return new EfficientLengthSkipIterable<E>(iterable, count); 585 return new EfficientLengthSkipIterable<E>(iterable, count);
586 } 586 }
587 return new SkipIterable<E>._(iterable, count); 587 return new SkipIterable<E>._(iterable, count);
588 } 588 }
589 589
590 SkipIterable._(this._iterable, this._skipCount) { 590 SkipIterable._(this._iterable, int skipCount)
591 if (_skipCount is! int) { 591 : _skipCount = _checkSkip(skipCount);
Lasse Reichstein Nielsen 2017/04/28 06:25:23 How about inlining the check: : _skipCount = ((s
sra1 2017/04/28 18:38:36 The trouble with combined conditions (&&, ||) is t
592 throw new ArgumentError.value(_skipCount, "count is not an integer"); 592
593 static int _checkSkip(int skipCount) {
Lasse Reichstein Nielsen 2017/04/28 06:25:23 If not inlined, maybe name this _checkCount and re
sra1 2017/04/28 18:38:37 Done.
594 if (skipCount is! int) {
595 throw new ArgumentError.value(skipCount, "count is not an integer");
Lasse Reichstein Nielsen 2017/04/28 06:25:23 Second argument is the argument name, not a messag
sra1 2017/04/28 18:38:36 Acknowledged.
593 } 596 }
594 RangeError.checkNotNegative(_skipCount, "count"); 597 RangeError.checkNotNegative(skipCount, "count");
Lasse Reichstein Nielsen 2017/04/28 06:25:23 Could we combine the two: if (skipCount is! int |
sra1 2017/04/28 18:38:37 See above. dart2js does much better at understandi
Lasse Reichstein Nielsen 2017/05/01 05:32:00 I'm a little concerned about writing code to match
598 return skipCount;
595 } 599 }
596 600
597 Iterable<E> skip(int count) { 601 Iterable<E> skip(int count) {
598 if (_skipCount is! int) { 602 if (_skipCount is! int) {
sra1 2017/04/28 18:38:37 Why is this checking _skipCount instead of count?
Lasse Reichstein Nielsen 2017/05/01 05:32:00 That looks like a bug. Good catch.
599 throw new ArgumentError.value(_skipCount, "count is not an integer"); 603 throw new ArgumentError.value(_skipCount, "count is not an integer");
600 } 604 }
601 RangeError.checkNotNegative(_skipCount, "count"); 605 RangeError.checkNotNegative(_skipCount, "count");
602 return new SkipIterable<E>._(_iterable, _skipCount + count); 606 return new SkipIterable<E>._(_iterable, _skipCount + count);
sra1 2017/04/28 18:38:36 .skip(1).skip(-1) 'works'
Lasse Reichstein Nielsen 2017/05/01 05:32:00 Iterable.skip is documented as requiring a non-neg
603 } 607 }
604 608
605 Iterator<E> get iterator { 609 Iterator<E> get iterator {
606 return new SkipIterator<E>(_iterable.iterator, _skipCount); 610 return new SkipIterator<E>(_iterable.iterator, _skipCount);
607 } 611 }
608 } 612 }
609 613
610 class EfficientLengthSkipIterable<E> extends SkipIterable<E> 614 class EfficientLengthSkipIterable<E> extends SkipIterable<E>
611 implements EfficientLengthIterable<E> { 615 implements EfficientLengthIterable<E> {
612 EfficientLengthSkipIterable(Iterable<E> iterable, int skipCount) 616 EfficientLengthSkipIterable(Iterable<E> iterable, int skipCount)
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
762 * Creates errors throw by [Iterable] when the element count is wrong. 766 * Creates errors throw by [Iterable] when the element count is wrong.
763 */ 767 */
764 abstract class IterableElementError { 768 abstract class IterableElementError {
765 /** Error thrown thrown by, e.g., [Iterable.first] when there is no result. */ 769 /** Error thrown thrown by, e.g., [Iterable.first] when there is no result. */
766 static StateError noElement() => new StateError("No element"); 770 static StateError noElement() => new StateError("No element");
767 /** Error thrown by, e.g., [Iterable.single] if there are too many results. */ 771 /** Error thrown by, e.g., [Iterable.single] if there are too many results. */
768 static StateError tooMany() => new StateError("Too many elements"); 772 static StateError tooMany() => new StateError("Too many elements");
769 /** Error thrown by, e.g., [List.setRange] if there are too few elements. */ 773 /** Error thrown by, e.g., [List.setRange] if there are too few elements. */
770 static StateError tooFew() => new StateError("Too few elements"); 774 static StateError tooFew() => new StateError("Too few elements");
771 } 775 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698