-
Notifications
You must be signed in to change notification settings - Fork 95
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
Collection implements Traversable. Wny not Iterator? #141
Comments
How about using https://www.php.net/manual/en/class.iteratoriterator.php? |
Keep in mind that your iterator will be affected by future updates to the data structure. I am working on improving this in 2.x but I see no reason why we can not apply a version of that to 1.x as well. Please see #17 |
@enumag , @rtheunissen , thank you! did you try that? i've just tried and got an interesting error:
when the execution gets to the next() call, the php stops abnormally. browser (chromium) reports:
in apache error_log (i use xampp on ubuntu) i see the following:
(didn't find any dump in /opt/lampp btw) if you uncomment the rewind() line, the error doesn't occur, everything works fine. |
I'll look into that segfault but I think you have to call |
cannot find in php docs that one MUST call anyway, even if it produces an error, this has to be a php error, throwable of something, of couse, not segfault obviously |
i only found a
it states only that foreach DOES call rewind, not that everyone MUST call it |
@githubeing At least for many SPL iterator calling rewind() is necessary. Sometimes iterators may work without it, but if they do, that's just a lucky accident. (Of course there should never be a crash due to a missing rewind() call, but there may be misbehavior.) |
i'm convinced that such things (e.g. such as required order of method calls (such that if you don't follow it, you'll get an error)) have to be explicitly stated in the docs. imo one of the bad sides of php itself (not ds ext) is the lack of consistent strictly defined contracts on which a dev could rely when something that should fail still works for some magic reason, then tomorrow it may suddenly stop working and you'll never now why (cause you didn't know why it worked). if rewind MUST be called first, then any access to any other method MUST produce error if |
therefore, i propose to state explicitly at least in ext-ds php docs that rewind has to be called first. |
You state that you intentionally made all classes final to enforce composition over inheritance. Map implements Traversable. I want to make a class that will contain a Map and a Generator and that will somehow iterate over them (how exactly - isn't important here). But it's currently impossible because Map implements Traversable and not Iterator. This means I may foreach it, but I cannot iterate it on a single step forward (next(), current()). I think this hinders me from using Map in composition. Could you implement it?
Why at all did you decide that implementing Traversable but not implementing Iterator is a good idea? (so that one could foreach all Map at a time but could not do it step by step) What is the purpose of this decision?
The text was updated successfully, but these errors were encountered: