-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provide an Iterable/Iterator protocol #148
Comments
Also worth pointing out that we'd want to make the following
|
Hm, |
Not only that, but any solution which separates into Java's The Java people have realized this in the design of spliterators; their answer seems to be tryAdvance, which (atomically) runs its callback parameter and returns |
Let's say someone wanted to implement a
Range
type in 007. (In a world where #32 is a fact, let's say.)Cool, but when they try to loop over their new range, it won't work, because
Q::Statement::For
lovesArray
s and nothing else:I propose changing the check from
Val::Array
to a duck-typing check for a method called.iterator
. Under a #33 scheme, the check would be against this kind of interface:As to the interface of an
Iterator
, there are a few established patterns out there:next
method returning an object with adone
property and (ifdone
is false) avalue
property.next
method which either returns the next value or throws aStopIteration
exception when it's out.next
method (which can throw aNoSuchElementException
if you iterate too much), and a separatehasNext
method to check beforehand.Note that these three are three separate solutions to the semipredicate/out-of-band problem — you can't use any particular value to signify "no more elements", because any value is a possible element.
But all of them are fumbling, incomplete implementations of an enum (see #34), so let's try to do it with sum types.
This puts people who use
case
matching in a very nice position where there's no way to accidentally forget to handle theIteratorEnd
case.I'm not sure we will make enums and
case
statements core yet. If we don't, then there needs to be a way to accessNextValue
without acase
statement. (But that way needs to be forbidden once the user imports enums and case statements.) Hm, I guess that's a weak case for enums in core.The text was updated successfully, but these errors were encountered: