Re: [RFC] Generators
| From: | Rasmus Lerdorf | Date: | Sat, 18 Aug 2012 14:28:20 +0000 |
| Subject: | Re: [RFC] Generators | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-62243@lists.php.net to get a copy of this message | ||
On 08/18/2012 10:12 AM, lester@lsces.co.uk wrote:
> Since this is yet another area where 'one does not have to use it if one
> does not want to' ... FLAGGING to the other users that a function is a
> 'special one' rather than just a normal function with a generator function
> seems to me just a necessity? HAVING to work through a functions code
> simply to see if it contains a 'yeild' so as to understand that it's not
> going to give a normal return seems insane? Alright the function can be
> properly documented with a docblock, but if it isn't ...
I don't see how that is any different from having to look through a
function to see if it is a void function or if it returns something, and
if so, what it returns. Or checking to see if it outputs something.
PHP is very dynamic. A function can be both void and non-void at the
same time. It can return mixed types and do pretty much anything you can
imagine. I don't really see what the generator keyword achieves. What
happens if I have a function that I mark as a generator but then I don't
put a yield in it? Is that a compile-level error? That might work, but
then what if I put a yield in it but the logic of the function never
gets to it:
if(cond) return [1,2,3];
else if(other_cond) yield $val;
else return;
There is no way to know at compile-time whether the yield will ever be
reached.
To me it seems odd to have a generator keyword that cannot be enforced
and doesn't mean anything other than stating a developer's intent.
Whether the developer actually carried through and implemented the
intended generator can only be verified by reading the code. This smells
much more like a source comment to me.
If we were going to go with a generator keyword, then I think the
feature needs to be completely re-thought. I would suggest dropping the
yield keyword entirely and having return be equivalent to a yield if the
function is marked as a generator. Then the keyword actually does something.
-Rasmus