On 2012-08-30 20:39, Derick Rethans wrote:
On Wed, 29 Aug 2012, Gustavo Lopes wrote:
On Wed, 29 Aug 2012 22:10:52 +0200, Derick Rethans <der...@php.net> wrote:
Nothing in the core throws an exception, why would this?!
This is not accurate. All the iterators throw exceptions on similar
situations. Generators are iterators, so I see no deviation from the
norm here.
Nothing shows that they are. In any example I saw, I don't even see any
OO syntax/functionality. For me, "yield" is a core syntax *keyword*, and
hence: no exceptions.
cheers,
Derick
I'm generally of the same opinion as you - issue a warning and skip the
second loop - but for the fact that the "idiomatic" use
<?php
foreach(generator() as $k => $item)
{
...
}
?>
is equivalent to the more explicit "manual" use
<?php
$generator = generator();
$generator->rewind();
while($generator->valid())
{
$k = $generator->key();
$item = $generator->current();
...
$generator->next();
}
?>
Since explicit control of generators can be useful, they won't
necessarily be wrapped in the foreach() idiom; would not exceptions be
the expected behaviour then?
There might be a compromise - trigger a warning in idiomatic use, an
exception in manual use - but that leaves a nasty taste in the mouth and
would just cause more problems later.
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php