Re: [RFC] Generators

From: Date: Tue, 14 Aug 2012 17:59:03 +0000
Subject: Re: [RFC] Generators
References: 1 2 3 4 5  Groups: php.internals 
Request: Send a blank email to internals+get-62152@lists.php.net to get a copy of this message
On 8/14/12 10:36 AM, Stas Malyshev wrote:
Hi!
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.
It does, since foreach uses rewind. So first rewind should succeed if you want iterators be usable in foreach. OTOH, on something like DB result set, next rewind does not make any sense, if you have non-seekable cursor, since the results consumed are gone and there's no way to get them back (you could rerun the query, but it might have side effects and nobody guarantees you'd get the same result anyway). So it makes sense for the generator to succeed on first rewind but fail on next ones. Note that generators by nature are stateful objects, so it is not unexpected that they would produce different result in different states. 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.

« previous php.internals (#62152) next »