Re: Re: [PHP-DEV] [VOTE] Generators
| From: | Morgan L. Owens | Date: | Fri, 31 Aug 2012 00:36:00 +0000 |
| Subject: | Re: Re: [PHP-DEV] [VOTE] Generators | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-62600@lists.php.net to get a copy of this message | ||
On 2012-08-30 20:39, Derick Rethans wrote:
On Wed, 29 Aug 2012, Gustavo Lopes wrote:<?php foreach(generator() as $k => $item) {On Wed, 29 Aug 2012 22:10:52 +0200, Derick Rethans <derick@php.net> wrote: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" useNothing 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.
...} ?> 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.