On Wed, Feb 26, 2020 at 4:47 AM Nikita Popov <nikita....@gmail.com> wrote:
>
> Hi internals,
>
> Generators currently do not support rewinding -- or rather, only support it
> if the generator is at/before the first yield, in which case rewinding is a
> no-op.
>
> Generators make it real breeze to implement primitives like
>
> function map(callable $function, iterable $iterable): \Iterator {
>     foreach ($iterable as $key => $value) {
>         yield $key => $function($value);
>     }
> }
>
> without having to do through the whole Iterator boilerplate. However, if
> you do this, you end up with an iterator that is not rewindable. If you
> want to make map() rewindable, you need to go back to a manual Iterator
> implementation. As iterators in PHP are assumed to be rewindable by
> default, this is somewhat annoying.
>
> 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.
>
> Regards,
> Nikita

Making generators "rewindable but don't really know if it's going to
work" is worse than "not rewindable," in my opinion. It is true that
it isn't very different from other iterators; you aren't going to know
if it's going to work.

At risk of hijacking the thread: can we extend the ability to always
rewind an iterator as long as it has not been progressed to _all_
iterators? It's not something I've seen discussed much, but it
actually has a really important characteristic: you can find out if
the iterator will yield at least 1 value (aka it's not empty) if the
iterator has this characteristic. Generators have this characteristic
but there are iterators in core that don't do this, and I'm sure there
are userland iterators that also don't do this. I'm wondering if we
can somehow force it to occur so nobody has to actually code that
semantic into every iterator. To me that would be even more valuable
than making generators semi-rewindable.

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to