Re: [RFC] Generators
| From: | Nikita Popov | Date: | Sat, 18 Aug 2012 10:03:57 +0000 |
| Subject: | Re: [RFC] Generators | ||
| References: | 1 2 3 4 5 6 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-62236@lists.php.net to get a copy of this message | ||
On Tue, Aug 14, 2012 at 7:59 PM, Aaron Holmes <aaron@aaronholmes.net> wrote:
> Thanks for clarifying. It makes sense now, considering foreach's behavior
> and the generators statefulness allowing what otherwise seems inconsistent.
> However, might it make sense to no-op instead of erroring? If generators
> allow rewind(), it would be unexpected to receive an error for calling it.
A no-op is the current behavior. The issue with that is that you could
accidentally reuse an already depleted (or partially depleted)
generator. I.e. you run it through one loop and then through another
one. With a no-op rewind it would be hard to figure out what the issue
is. The error makes clear that you tried to iterate an already
iterated generator.
Furthermore, if you indeed *want* to partially traverse a generator in
several loops, you can simply wrap it in a NoRewindIterator. This has
the additional benefit of making it more clear what you want to do.
Nikita