On Mon, Aug 20, 2012 at 3:48 PM, Rasmus Lerdorf <ras...@lerdorf.com> wrote:

> On 08/20/2012 07:56 AM, Ángel González wrote:
> > On 20/08/12 02:01, Rasmus Lerdorf wrote:
> >> I would still like to understand what this generator keyword would
> >> actually do. I don't see how it would work. Would a function marked
> >> generator somehow not be allowed to return normally or to finish and not
> >> return anything? How could this be enforced? I am completely against any
> >> keyword that is essentially documentation-only.
> >>
> >> -Rasmus
> > Given that such function could "return several times", seems a different
> > enough function type to have its keyword.
> > You could not decorate it and rely instead on the presence of the yield
> > keyword, but parsers will thank knowing about it from the start rather
> > than realising at mid-parsing that the function is a completely
> > different beast.
>
> So how about something like this:
>
> generator function f() {
>   echo "Hello World";
> }
>
> generator function f() {
>   return 1;
> }
>
> generator function f($arg) {
>   if(f!$arg) yield 1;
>   else if($arg<0) return 1;
>   else return;
> }
>
> What does the generator keyword mean in each of these cases? Anything?
> Nothing? Would I see a difference either at compile-time or at execute
> time if I left it out?
>
> -Rasmus
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
I still don't think that we shall separate it into two different keywords,
and even if so - we shall still reserve the ability to use return.

Think of this case:
function keywordsInFile($file, $keyword) {
      if ( ($han = fopen($file, 'r') === FALSE ) {
               return; // <-- this is better than starting to wrap the
function with if-else
      }
      while(!feof(...)) {
              foreach ($keywords as $k) {
                      if ( strpos($k, $line) !== FALSE ) { yield $k; }
              }
      }
}

Though it's not the best example and I can think about more complicated -
it can reveal my point - I wish to use the "return" keyword in order to
stop the current function run. In many complicated sites you got many
if-else cases in one function that when the statement evaluates to
true/false you shall return from the function.
Structure like
if ( ... ) {
      if ( ! ... ) {
            if ( .... ) {
                  if ( ... ) {
                        yield $v;
                  }
            }
      }
}

is ugly and make the program harder to read than
if ( ! ... ) return;
if ( ... ) return;
if (! ... ) return;
if ( ! ... ) return;
yield $k;

Reply via email to