Re: Generators in PHP
| From: | Nikita Popov | Date: | Wed, 08 Aug 2012 20:42:23 +0000 |
| Subject: | Re: Generators in PHP | ||
| References: | 1 2 3 4 5 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-62088@lists.php.net to get a copy of this message | ||
On Wed, Aug 8, 2012 at 10:27 PM, Andrew Faulds <ajf@ajf.me> wrote:
> Hi Nikita,
>
> 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.
Without parenthesis their behavior in array definitions and nested
yields is ambigous:
array(yield $key => $value)
// can be either
array((yield $key) => $value)
// or
array((yield $key => $value))
yield yield $key => $value;
// can be either
yield (yield $key) => $value;
// or
yield (yield $key => $value);
Apart from that particular case there is the general operator
precedence inclarity, e.g.
yield $foo . $bar;
// could be
(yield $foo) . $bar;
// or
yield ($foo . $bar);
This obviously is not a problem per-se, but with yield-style unary
operators the precedence rules are often hard to figure out. E.g. most
people would probably think that
if (include('foo.php') == true) { ... }
would be check the return value of include, but it's actually not.
Rather it includes the file ('foo.php') == true, which is ''.
Another issue was a purely implementational: In order to support
by-ref yielding I have to distinguish between variable and
non-variable expressions, which breaks the usual precedence rules. I
was not able to fix the shift/reduce conflicts that are created by
this without requiring the parenthesis.
Also I'd like to mention that Python also has the paren-requirement
for yield-expressions. It even requires parens for a simple yield,
i.e. you'd have to write "data = (yield)". This is not necessary in
PHP, because PHP has semicolons.
Nikita