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 propose the opposite: that Iterable.generate makes the argument required.
It's a bad design for a strongly typed language that Iterable<String>.generate(5) is allowed (and fails to cast an int to String at runtime).
To create a range of integers, there should be a function, method or class somewhere specialized for that.
Say
classRangeextendsIterable<int> {
finalint _start, _end;
constRange(int start, int end) : _start = start, _end = start <= end ? end : start;
intget length => _end - start;
boolgetcontains(Object? other) => other isint&& _start <= other && other <= end;
Iterator<int> get iterator =>_RangeIterator(_start, _end);
}
class_RangeIteratorimplementsIterator<int> {
int _next, _end;
int? _current;
RangeIterator(this._next, this._end);
intget current => _current ?? (throwStateError("No element"));
boolmoveNext() {
if (_next < _end) {
_current = _next;
_next++;
returntrue;
}
_current =null;
returnfalse;
}
}
That could be useful in many ways, and doesn't need an unsoundly typed constructor on Iterable.
To get a sequence of integers in a List, we must do either of two suboptimal choices:
Iterable<int>.generate()
and usetoList()
:List<int>.generate()
and explicitly type the identity function:I propose
List.generate()
on integers has the identity function by default, likeIterable.generate()
.The text was updated successfully, but these errors were encountered: