On Wed, 26 Feb 2020 at 15:23, Nikita Popov <[email protected]> wrote:
> Point of order: foreach() always rewinds the array / Iterator it gets. The
> code does work correctly under the proposed scheme.
>
Ah, I'd missed that, sorry; that makes the entire first half of my e-mail
irrelevant. :)
The proposed behaviour is actually closer to current behaviour than I
thought, because the initial code before the first yield actually runs when
you first rewind the iterator:
function foo() {
echo 'Begin';
yield 42;
}
$f = foo(); // Doesn't echo yet
$f->rewind(); // Now echoes 'Begin'
$f2 = foo();
foreach ( $f2 as $x ) {
// echoes 'Begin' before first loop
echo $x;
}
Just to check, would the proposed behaviour still special-case a generator
which hasn't been progressed?
$f = foo();
$f->rewind(); // echoes 'Begin'
$f->rewind(); // this would still do nothing?
$f->next();
$f->rewind(); // this currently throws an error, and would echo 'Begin'
again?
Regards,
--
Rowan Tommins
[IMSoP]