Re: Support rewinding of generators
| From: | Andrea Faulds | Date: | Sun, 01 Mar 2020 13:54:00 +0000 |
| Subject: | Re: Support rewinding of generators | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-108796@lists.php.net to get a copy of this message | ||
Hi,
Nikita Popov wrote:
There is a relatively simple (at least conceptually) way to make generators rewindable: Remember the original arguments of the function, and basically "re-invoke" it on rewind(). I'm wondering what people think about adding this functionality. I think the main argument against it is that not all generators may behave sensibly if you re-run their code -- there's probably a reasonable expectation that an iterator will return the same sequence of values are rewinding, something which we cannot guarantee with generators, but also don't enforce with normal iterators either.I am not sure if I think this is a good idea… for one thing it may not be necessary to add support in core for this, because you could easily write a userland Iterator class that wraps a generator-returning closure and (re-)invokes it for you. If you do it yourself that way, it's clear what's actually happening behind the scenes at least. If it must be done, perhaps the code must explicitly opt-in to being rewindable somehow? That would avoid problems with rewinding generators which aren't designed for it. Thanks, Andrea