RE: [PHP-DEV] Re: Generators in PHP

From: Date: Wed, 25 Jul 2012 21:00:30 +0000
Subject: RE: [PHP-DEV] Re: Generators in PHP
References: 1  Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message
 

> -----Original Message-----
> From: Nikita Popov [mailto:[email protected]] 
> Sent: 22 July 2012 16:53
> To: Jared Williams
> Cc: Nikita Popov; PHP internals
> Subject: Re: [PHP-DEV] Re: Generators in PHP
> 
> On Sat, Jul 21, 2012 at 6:31 PM, Jared Williams 
> <[email protected]> wrote:
> > Can't yield a reference to an array item directly.
> >
> > Eg.
> >
> > function &map(array &$row)
> > {
> >      yield $row[0];
> > }
> 
> Thanks, this is fixed now.
> 
> > Also seems to be a problem with iterating
> >
> > foreach(map($row) as &$value)
> >
> > cannot be done without a fatal error
> >
> > $i = map($row);
> > foreach($i as &$value)
> >
> > however works.
> 
> This was an old foreach restriction that never really made 
> sense and makes even less sense once generators are in. So I 
> dropped it. Now everything can be iterated by-reference.
> 
> > Seems relatively easy to trigger a infinite loop atm.
> >
> > Typo'd a SQL table name which caused an exception within 
> PDO inside a 
> > generator function, which then caused an infinite loop.
> 
> I forgot to rethrow the exception in the foreach scope. 
> Should be fixed now.
> 
> Thanks for your feedback!

No problem. 

Though here is seemingly another problem, though it could be within
spl's MultipleIterator()

function a()
{
	yield 'a';
	yield 'aa';
}

function b()
{
	yield 'b';
	yield 'bb';
}

$m = new MultipleIterator();
$m->attachIterator(a());
$m->attachIterator(b());
foreach($m as $v)
{
	list($a, $b) = $v;
	var_dump($a, $b);
}

causes a seg fault, whereas the vanilla array version

$m = new MultipleIterator();
$m->attachIterator(new ArrayIterator(['a', 'aa']));
$m->attachIterator(new ArrayIterator(['b', 'bb']));
foreach($m as $v)
{
	list($a, $b) = $v;
	var_dump($a, $b);
}

works fine. 

Jared




Thread (142 messages)

« previous php.internals (#61794) next »