Re: [RFC] Generators
| From: | Aaron Holmes | Date: | Tue, 14 Aug 2012 16:42:35 +0000 |
| Subject: | Re: [RFC] Generators | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-62147@lists.php.net to get a copy of this message | ||
On 8/13/12 9:09 AM, Nikita Popov wrote:
On Sun, Aug 12, 2012 at 10:08 PM, Brian Moon <brian@moonspot.net> wrote:That said, rewind() should behave consistently. I don't feel it makes sense to have rewind() succeed at one point, and fail at another. It would only cause confusion when not familiar with the behavior. Either allow it, or don't. Not both. Thanks, Aaron HolmesAlso, not allowing rewinding is unintuitive for something that is an iterator in PHP. If I can foreach() it and I can call next() on it, I expect to be able to reset() it as well. IMO, you would need to issue a FATAL PHP error if that simply is not allowed. Or you have to have a second syntax for what to do in that case. At that point, you are implementing Iterator.Currently I'm planning to implement the following behavior for rewind(): * If before first yield: Resume to first yield (this priming behavior is common to all the Iterator methods) * At first yield: No-op * After first yield: Recoverable fatal error So this would allow you to call ->rewind() after creating the generator, but will throw an error if you try to do so later. My perspective is that generators are intended to generate and yield a result, not so much to iterate over a known set of results. Thus, the results not really being known, there is nothing to rewind to. Rewinding would also presumably require more state control, or keeping track of previous results, and defeat the memory advantages of generators. Perhaps this is erroneous?