On Mon, Mar 11, 2019, at 8:16 AM, Nikita Popov wrote:
> On Sun, Mar 10, 2019 at 10:45 PM Larry Garfield <[email protected]>
> wrote:
>
> > Hello, peoples. I know it's been discussed once or twice before on the
> > list, many years ago, but not recently. I therefore feel OK putting forth
> > the following draft proposal for Comprehensions, aka "compact generators",
> > in PHP:
> >
> > https://wiki.php.net/rfc/cohttps://externals.io/message/61021mprehensions
> > <https://wiki.php.net/rfc/comprehensions>
> >
> > Sara Golemon has written a preliminary patch that is partially complete
> > (see the RFC link for details, it's impressively simple), but unfortunately
> > doesn't have the bandwidth to complete it at this time. I am therefore
> > looking for collaborators with more knowledge of internals than I (which is
> > almost everyone) to help finish it up.
> >
> > The syntax proposed is also subject to revision if a terser but still
> > lexer-friendly alternative can be proposed.
> >
> > At the moment I'm not calling this Proposed yet, as I don't feel
> > comfortable doing so until someone else is on board to finish coding it.
> > That said, if someone wants to weigh in on the concept for now (hopefully
> > supportively) that's also fine.
> >
> > Anyone excited enough to help finish the job?
> >
> > (This is my first RFC collaboration, so if you're going to smack me for
> > goofing something please be gentle about it.)
> >
>
> I've brought up a similar proposal a few years ago, probably around the
> time generators were first introduced: https://externals.io/message/61021
Ah, I knew I'd seen it on the list once before. Thanks.
> I think my main point of feedback would be to stick closer to existing PHP
> syntax, even if it costs us some brevity. I would prefer
>
> $gen = [foreach ($list as $x) if ($x % 2) yield $x * 2];
>
> over
>
> $gen = [for $list as $x if $x % 2 yield $x * 2];
>
> The latter is nice in languages that generally use "for" for this purpose
> and generally allow omitting parentheses, but I don't think it's good to
> introduce this kind of syntax inconsistency in one place.
>
> Similarly, I found the ability to omit the "yield" expression somewhat
> confusing. Especially once you get to nesting
>
> $gen = [for $a as $b for $b as $c];
In that case you'd get a list of $c returned (implicit yield $c), although I
can see where that's non-obvious. OTOH, requiring the yield statement every
time in the majority case seems like excessive verbosity and ceremony.
> the behavior becomes non-obvious. Also wondering how this part interacts
> with keys, do you decide whether or not to yield the keys based on whether
> they are part of the for expression?
>
> $gen = [for $list as $v]; // drops keys
> $gen = [for $list as $k => $v]; // keeps keys
Yes, that's the intent. No yield means implicit "key and value"; if key is
omitted then it's implicitly just 0-based numbers.
> Finally, Python makes a distinction between list comprehensions using []
> and generator expressions using (). This proposal effectively corresponds
> to generator expressions, but uses the [] syntax. I'm wondering if that
> will cause confusion.
>
> Regards,
> Nikita
I don't think so. For one, although we started from Python syntax the result
is decidedly not Python, so I wouldn't expect people to assume the same symbols
mean the same things. For another, Python uses [] for a list-producing
comprehension, and {} for a dictionary-producting comprehension. Whether those
return generators or not is a version question, not a syntax question. Since
PHP doesn't differentiate between lists and dictionaries to begin with there's
no need for different syntax. Our "dictionaries" already use [].
--Larry Garfield
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php