Re: Generators in PHP

From: Date: Wed, 08 Aug 2012 20:55:07 +0000
Subject: Re: Generators in PHP
References: 1 2 3 4 5  Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message
On 08/08/12 21:43, Stas Malyshev wrote:
Hi! I'm not sure $data = (yield $value) makes a lot of sense. Why we have two variables here? Why it is only operator in the language that requires parentheses around? All these complex parentheses rules seem unnecessarily complicated. I'd rather do it in a more simple way: yield in an expression means incoming yield. I.e.: $data = yield; foo(yield, 1, 2); list($a, $b) = yield; Same with: call(yield $value) - what is the meaning of $value here? Why not just call(yield)? I would also not support array((yield $key => $value)) - it seems to be really unclear how it works. I'd just have yield produce a value which was sent, and that's it, and you could use this value in the same way you'd use any other expression. Only question I have here is what happens if you use yield in the middle of function call expression, for example: call(foo(bar(), $foo, yield, 3), baz()); We are kind of stopping the function in the middle of the function call and going to unrelated code here, and may never return back. Where the required cleanups, etc. should happen? Another question is, if my function is like this: function foo() { var_dump(yield); } And I do: foo()->current() - what happens? Do I get null there? What happens if after that I do send() on the same generator? In general, interaction between incoming and outgoing yields is not very clear, especially what happens when you combine them. Also, small point: yield is repeatably called "statement" in the doc, but in fact it can be both statement and expression with returning yield. Hmm. This is just a quick thought:
Considering the yield syntax will vary about needing () round it, why not make it a "fake" function (language construct). This way it's consistent: yield(), yield($v), yield($k => $v), $a = yield(), etc. (yield $x) is just messy as an expression. We don't have (isset $x), we have isset($x). -- Andrew Faulds http://ajf.me/

Thread (142 messages)

« previous php.internals (#62090) next »