Re: Generators in PHP
| From: | Andrew Faulds | Date: | Wed, 08 Aug 2012 20:27:26 +0000 |
| Subject: | Re: Generators in PHP | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-62087@lists.php.net to get a copy of this message | ||
On 08/08/12 21:14, Nikita Popov wrote:
On Fri, Jul 27, 2012 at 8:09 PM, Nikita Popov <nikita.ppv@gmail.com> wrote:I notice you require brackets round yield $k => $v and yield $v. Is this the formal syntax, i.e. '(' T_YIELD var => var ')'? If so it makes sense in a way, but it feels a little hackish. Does yield $v cause some sort of parsing issue that putting it in brackets solves? I realise that it's less ambiguous, but I don't like the idea of it. PHP's syntax has enough special cases already IMO. Regards, -- Andrew Faulds http://ajf.me/I now added the mentioned sections: https://wiki.php.net/rfc/generators#yield_keyword https://wiki.php.net/rfc/generators#sending_values I also added a list of error conditions: https://wiki.php.net/rfc/generators#error_conditions Sorry for the delay, Nikita Hi Nikita,5. Are multiple yields allowed? I.e. the rfc mentions something like yield yield $a - what that would mean? I'd allow yield only be applied to variable expression (lval) because double yield doesn't make sense to me, but maybe I miss something.yield yield $a would basically first yield $a and then receive some value (via send) and yield that value again. Nothing you'd normally do ;) It was mentioned only as a syntax ambiguity consideration. Actually I added some additional parenthesis requirements for yield. For example you'd have to write the above asyield (yield $a)now (which should be slightly more clear). I haven't yet reflected this change in the RFC, but I'll add a section on it later.6. “Sending values” section seems to be missing. Especially useful would be to cover what happens with keys there and what is the syntax there - is it just "$a = yield;"? Or does it mean when yield is used in expression it becomes incoming yield? And, last but not least - do we need sending into generators at all?Yeah, I haven't written that section yet. But it is fairly simple: If you go $generator->send($foo) then $foo will be the result of the currentyieldexpression. And yes, this also works with keys and values. All of the following are valid:$data = yield; $data = (yield $value); $data = (yield $key => $value);The first case is the most common though. I.e. you usually use it either as a generator or a reverse generator, not both. But doing both is also common for cooperative multitasking etc. Regarding the last question: I think the feature is worth adding. It is a very powerful concept that is hard to implement otherwise. Useful in particular for things like parsing and multitasking.6. What happens if you send into a by-ref generator? Is the data sent by-ref then? What if it's an expression that can't be send by-ref?No, sending is always by-value. By-ref only affects the yielding part.