On Sun, 10 Mar 2019 at 22:45, Larry Garfield <la...@garfieldtech.com> 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/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.) > > -- > Larry Garfield > la...@garfieldtech.com > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > Hello Larry, I do like the proposal of adding comprehensions to PHP, and I do agree with you that short closures do not replace a short closure synstax (as even with them it is still not ideal). However I have a couple of complaints, JS comprehensions have not become a standard and have been removed from my understanding [1][2]. Secondly I don't really understand the argument for using `for` instead of ` foreach` Sure it saves 4 chars but the PHP version compared to the Python uses `yield ` which already lengthen it, I do understand why it is there because it is a generator. But Python using `for` makes more sense, imho, because its syntax is `expression for item in iterator (if condition)`. Now maybe what I am going to say is total BS because I don't know *how* the engine works, but wouldn't also use foreach make it easier for the lexer as it already know the `as` keyword? Maybe if we want to mimic the Python syntax why not use a syntax like: `(yield expression) foreach iterator as item (if condition)` I also suppose that comprehension chaining is possible because it returns a generator. Small nitpick but shouldn't the `current` method link to the Generator->current instead of the function `current` which only works with arrays? Also would it be possible to use a string as an iterable expression? Because currently it's not possible to foreach a string (but that's maybe more the scope of another RFC). Best regards George P. Banyard [1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Array_comprehensions [2] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Generator_comprehensions