Re: Generators in PHP

From: Date: Thu, 09 Aug 2012 15:59:26 +0000
Subject: Re: Generators in PHP
References: 1 2 3 4 5 6 7  Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message
On 09/08/12 16:58, Nikita Popov wrote:
On Wed, Aug 8, 2012 at 10:55 PM, Andrew Faulds <[email protected]> wrote:
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).
There are two reasons why I would not choose that syntax: 1. This would make "yield" look like a functions, without actually being a function. PHP has done this in the past quite often and I think it was a mistake. It is the reason why people try to write empty(someCall()). It looks like a function, so they expect it to behave like one. Similarly you also can't do $f = 'empty'; $f($foo), which again is confusing to people new to the language. yield(), so far as the programmer is concerned, might as well be a function. It isn't, strictly speaking, but like other function calls, it suspends execution of the function until the called function completes.
So I don't think this is a problem.
2. Other languages that implement generators also use the "yield $foo" syntax (and also have the same parentheses requirements). So this makes PHP consistent with them. Doesn't mean we can't lead the way and have a nicer syntax :)
Nikita
-- Andrew Faulds http://ajf.me/

Thread (142 messages)

« previous php.internals (#62106) next »