Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The current protocol is very comparable to Python, where
.__iter__()
returns an iterator object which implements.__next__()
and throwsStopIteration
on completion.Option
is much cleaner than using a exceptions as a flow control hack though. It requires that the container is frozen so there's no worry about invalidating them.Advantages over internal iterators, which are functions that are passed closures and directly implement the iteration protocol:
TreeMap
andTreeSet
implementations, but regions and traits weren't solid enough to make it generic yet.ZipIterator
, which also implement the same trait themselves.The disadvantage is that they're a pain to write without support from the compiler for compiling something like
yield
to a state machine. :)This can coexist alongside internal iterators since both can use the current
for
protocol. It's easier to write an internal iterator, but external ones are far more powerful/useful so they should probably be provided whenever possible by the standard library.Current issues
#5801 is somewhat annoying since explicit type hints are required.
I just wanted to get the essentials working well, so I haven't put much thought into making the naming concise (free functions vs. static
new
methods, etc.).Making an
Iterable
trait seems like it will have to be a long-term goal, requiring type system extensions. At least without resorting to objects which would probably be unacceptably slow.