2012/7/20 Nikita Popov <nikita....@gmail.com>:
> On Tue, Jun 5, 2012 at 7:35 PM, Nikita Popov <nikita....@googlemail.com> 
> wrote:
>> Hi internals!
>> The implementation is outlined in the RFC-stub here:
>> https://wiki.php.net/rfc/generators
>
> A small progress update on this:

> * Generators are now automatically detected by the presence of "yield"
> instead of requiring the "*" modifier.

My first thought was how could someone reading the code see, that it
is a generator?

I mean: The "yield" can be between 50 lines of code. How could someone
see that? That a machine can do this is out of question, but humans
can't see that. Why is this important? Because when I see such code as
in the RFC and overlook the "yield" I would think "WTF", how could the
code work like this?

Why would someone not see  this? Assume that the code was written and
is now quite old. The original developer is gone. A new one sits on
his place and now the customer says "hey, there is a bug anywhere".
The developer begins to search for it, and has no clue, how he can
reproduce it. Maybe he finds a part and he thinks now "Whoa, WTF" and
begins to debug that. After a while he will of course find "Oh, it's a
generator... just forgotten, that PHP can have that and the 'yield' is
so small in the middle of the function, nobody will see that..." But
he has spend some time to find that out.

That's what I think will happen. I think "yield" makes it more
difficult to maintain unknown code.

I think "yield" alone is too less. I think the function should be
explicitly marked as generator function.

Stupid Suggestions:
----------------------------

generator function gen()
{
    yield 1;
}

generator gen()
{
    yield 1;
}

function generator gen()
{
    yield 1;
}

Hm... not really satisfied with that.  For me this is a little bit
unlogical, that just a simple function can return an object.

So, how about implementing as a class? I mean: If it is returning a
class, why not implement it as class?


class gen implements generator
{
    function __generator()
        yield 1;
    }
}

class gen extends generator
{
    function __generator()
        yield 1;
    }
}

Calling looks for me now much less "magic":

$gen = new gen;
$gen->next();

As maintainer I see "oh, it's a magic method", it's such an iterator thingy.
Another advantage: It's not final. I could replace next() etc. with my
own implementation.
And it works more hand in hand with iterators.

Just my very primitive first thoughts as PHP-scripts-developer,
without knowing much about the implementations in other languages. :)


PS: I would like to see that yield goes hand in hand with the iterator-stuff.

PPS: Ok, maybe a change isn't really needed, but I think how to use
this in a good manner (to avoid the problems mentioned above) should
be part of the docs.

-- 
Regards, Alex Aulbach

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to