Re: Support rewinding of generators

From: Date: Wed, 26 Feb 2020 16:51:56 +0000
Subject: Re: Support rewinding of generators
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to internals+get-108772@lists.php.net to get a copy of this message
On Wed, 26 Feb 2020 at 15:23, Nikita Popov <nikita.ppv@gmail.com> 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]

« previous php.internals (#108772) next »