On Sun, Mar 10, 2019 at 3:45 PM 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
I want to like the idea of list comprehensions. However, after thinking about it for quite a bit over the past year as well as since discussion started, the issue is really that closures are terrible in PHP. Look at this example from the RFC: $result = (function() use ($table) { foreach ($table as $num => $row) { if ($num % 2 == 0) { foreach ($row as $col => $val) { if ($col >= 3) { yield $num => $val; } } } } })(); We could use this syntax today: $result = (function () { foreach ($table as $num => $row) if ($num % 2 == 0) foreach ($row as $col => $val) if ($col >= 3) yield $num => $val; })(); Compare that to this code using comprehensions, using the same whitespace style: $result = (for $table as $num => $row if $num %2 ==0 for $row as $col => $value if $col >= 3 yield $num => $val ); In my opinion there are no meaningful differences here, and nothing prevents you from using the all-in-one-line style if you care to. Let's look at a shorter piece, and let's also assume that the following code is eagerly evaluated and stored into an array (which is not what the RFC is proposing, but it helps for this example): $result = [ for ($hex_numbers as $num) yield intval($num, 16); ]; With closures as they are today: $result = array_map(function($num) { return intval($num, 16); }, $hex_numbers); Oof, that sucks and I didn't even have to bind any variables. But if we had shorter closures (this is JavaScript style) then it's fine again: $result = array_map(($num) => intval($num, 16), $hex_numbers); My point is, closures are the major culprit. We really need shorter closures. Again, I want to like comprehensions. I think we should revisit list comprehensions after we've had shorter closures for a little while (hopefully we can get something passed for 7.4 or 8.0). -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php