Re: [PHP-DEV] Re: [RFC] Arrow functions / short closures

This is only part of a thread. view whole thread
  105179
April 9, 2019 15:23 bjorn.x.larsson@telia.com (=?UTF-8?Q?Bj=c3=b6rn_Larsson?=)
Den 2019-04-09 kl. 12:19, skrev Nikita Popov:
> On Tue, Apr 9, 2019 at 8:56 AM Björn Larsson larsson@telia.com> > wrote: > >> Den 2019-04-08 kl. 16:06, skrev Nikita Popov: >> >>> On Wed, Mar 13, 2019 at 4:56 PM Nikita Popov ppv@gmail.com> >> wrote: >>>> Hi internals, >>>> >>>> Motivated by the recent list comprehensions RFC, I think it's time we >> took >>>> another look at short closures: >>>> >>>> https://wiki.php.net/rfc/arrow_functions_v2 >>>> >>>> This is based on a previous (withdrawn) proposal by Levi & Bob. It uses >>>> the syntax >>>> >>>> fn($x) => $x * $multiplier >>>> >>>> and implicit by-value variable binding. This example is roughly >> equivalent >>>> to: >>>> >>>> function($x) use($multiplier) { return $x * $multiplier; } >>>> >>>> The RFC contains a detailed discussion of syntax choices and binding >> modes. >>>> Regards, >>>> Nikita >>>> >>> Heads up: I plan to start voting on this RFC tomorrow if nothing new >> comes >>> up. >>> >>> Most of the discussion was (as expected) about the choice of syntax. >>> Ultimately I think there are many reasonable choices we can make here, >> but >>> we should stick to a specific proposal for the purposes of the RFC vote. >>> None of the given arguments convinced me that some other syntax is >>> *strictly* better than the proposed fn($x, $y) => $x*$y -- it's more a >>> matter of some choices being slightly better in one case and slightly >> worse >>> in another. My personal runner-up would be \($x, $y) => $x*$y, but I >>> suspect that there are some people who are more strongly biased against >>> "sigil salad" than I am... >>> >>> Nikita >> Hi, >> >> I recall that the perception of the ==> syntax in the Hacklang user >> community was quite positive. Of course it was implementation >> difficulties. Are they still to cumbersome in PHP? >> >> Personally I prefer that one being more readable or the \ one. >> Anyway, glad to see that short closures finally is on the road >> again! >> > The ==> syntax is the other one I implemented ( > https://github.com/php/php-src/pull/3945). The implementation is based on > lexer lookahead, which is ugly but still manageable. I haven't seen much > support for this variant in this discussion though. And of course, if > there's no strong preference for ==>, I'd rather go with the variant that > is easier for us (and all 3rd party tooling) to support from a technical > perspective. > > Nikita Maybe part of the reason it didn't attract to much support is that people
are quite aware and respect the implementation difficulties. I recall a comment in this thread pointing in that direction. Well it's probably me being a long-time PHP user and now trying to learn RUST that finds the fn() syntax a bit confusing ;-) From that perspective I would prefer the \$x => $x **2 vs fn($x) => $x **2 or the ==> one. r//Björn L
  105185
April 9, 2019 21:52 bjorn.x.larsson@telia.com (=?UTF-8?Q?Bj=c3=b6rn_Larsson?=)
Den 2019-04-09 kl. 17:23, skrev Björn Larsson:
> Den 2019-04-09 kl. 12:19, skrev Nikita Popov: >> On Tue, Apr 9, 2019 at 8:56 AM Björn Larsson larsson@telia.com> >> wrote: >> >>> Den 2019-04-08 kl. 16:06, skrev Nikita Popov: >>> >>>> On Wed, Mar 13, 2019 at 4:56 PM Nikita Popov ppv@gmail.com> >>> wrote: >>>>> Hi internals, >>>>> >>>>> Motivated by the recent list comprehensions RFC, I think it's time we >>> took >>>>> another look at short closures: >>>>> >>>>> https://wiki.php.net/rfc/arrow_functions_v2 >>>>> >>>>> This is based on a previous (withdrawn) proposal by Levi & Bob. It >>>>> uses >>>>> the syntax >>>>> >>>>>       fn($x) => $x * $multiplier >>>>> >>>>> and implicit by-value variable binding. This example is roughly >>> equivalent >>>>> to: >>>>> >>>>>       function($x) use($multiplier) { return $x * $multiplier; } >>>>> >>>>> The RFC contains a detailed discussion of syntax choices and binding >>> modes. >>>>> Regards, >>>>> Nikita >>>>> >>>> Heads up: I plan to start voting on this RFC tomorrow if nothing new >>> comes >>>> up. >>>> >>>> Most of the discussion was (as expected) about the choice of syntax. >>>> Ultimately I think there are many reasonable choices we can make here, >>> but >>>> we should stick to a specific proposal for the purposes of the RFC >>>> vote. >>>> None of the given arguments convinced me that some other syntax is >>>> *strictly* better than the proposed fn($x, $y) => $x*$y -- it's more a >>>> matter of some choices being slightly better in one case and slightly >>> worse >>>> in another. My personal runner-up would be \($x, $y) => $x*$y, but I >>>> suspect that there are some people who are more strongly biased >>>> against >>>> "sigil salad" than I am... >>>> >>>> Nikita >>> Hi, >>> >>> I recall that the perception of the ==> syntax in the Hacklang user >>> community was quite positive. Of course it was implementation >>> difficulties. Are they still to cumbersome in PHP? >>> >>> Personally I prefer that one being more readable or the \ one. >>> Anyway, glad to see that short closures finally is on the road >>> again! >>> >> The ==> syntax is the other one I implemented ( >> https://github.com/php/php-src/pull/3945). The implementation is >> based on >> lexer lookahead, which is ugly but still manageable. I haven't seen much >> support for this variant in this discussion though. And of course, if >> there's no strong preference for ==>, I'd rather go with the variant >> that >> is easier for us (and all 3rd party tooling) to support from a technical >> perspective. >> >> Nikita > Maybe part of the reason it didn't attract to much support is that people > are quite aware and respect the implementation difficulties. I recall a > comment in this thread pointing in that direction. > > Well it's probably me being a long-time PHP user and now trying to learn > RUST that finds the fn() syntax a bit confusing ;-) From that > perspective I > would prefer the \$x => $x **2 vs fn($x) => $x **2 or the ==> one. > > r//Björn L > > Hi again,
Just read the RFC a bit closer and saw that \$x is'nt possible, pity. Taking an example from chregu gist with arrow function inside a function call: - $waithandles = $this->urls->map(fn($url) => $this->fetcher->fetch($url)); - $waithandles = $this->urls->map(\($url) => $this->fetcher->fetch($url)); - $waithandles = $this->urls->map($url ==> $this->fetcher->fetch($url)); I would say that when lambda functions occurs in function calls I find the \ or ==> syntax more readable. Over & out //Björn L
  105186
April 9, 2019 22:10 robehickman@gmail.com (Robert Hickman)
> - $waithandles = $this->urls->map(fn($url) => $this->fetcher->fetch($url)); > - $waithandles = $this->urls->map(\($url) => $this->fetcher->fetch($url)); > - $waithandles = $this->urls->map($url ==> $this->fetcher->fetch($url)); > > I would say that when lambda functions occurs in function calls I find the > \ or ==> syntax more readable. >
To me, in that context, '==>' is the most readable as it does not have parentheses on the argument. It's a bit visually noisy with them.
  105190
April 10, 2019 08:01 markus@fischer.name (Markus Fischer)
On 10.04.19 00:10, Robert Hickman wrote:
>> - $waithandles = $this->urls->map(fn($url) => $this->fetcher->fetch($url)); >> - $waithandles = $this->urls->map(\($url) => $this->fetcher->fetch($url)); >> - $waithandles = $this->urls->map($url ==> $this->fetcher->fetch($url)); >> >> I would say that when lambda functions occurs in function calls I find the >> \ or ==> syntax more readable. >> > > To me, in that context, '==>' is the most readable as it does not have > parentheses on the argument. It's a bit visually noisy with them.
I concur, `==>` to me also stands out the most easiest to read without too much parenthesis noise. - Markus
  105192
April 10, 2019 08:33 gadelat@gmail.com (Gabriel O)
Those parentheses are important when having multiple argument

On 10 April 2019 10:02:46 AM Markus Fischer <markus@fischer.name> wrote:

> On 10.04.19 00:10, Robert Hickman wrote: >>> - $waithandles = $this->urls->map(fn($url) => $this->fetcher->fetch($url)); >>> - $waithandles = $this->urls->map(\($url) => $this->fetcher->fetch($url)); >>> - $waithandles = $this->urls->map($url ==> $this->fetcher->fetch($url)); >>> >>> I would say that when lambda functions occurs in function calls I find the >>> \ or ==> syntax more readable. >>> >> >> To me, in that context, '==>' is the most readable as it does not have >> parentheses on the argument. It's a bit visually noisy with them. > > I concur, `==>` to me also stands out the most easiest to read without > too much parenthesis noise. > > - Markus > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php
  105195
April 10, 2019 09:07 markus@fischer.name (Markus Fischer)
Hi, Gabriel,

On 10.04.19 10:33, Gabriel O wrote:
> Those parentheses are important when having multiple argument
Please don't top post, thanks! Thanks for pointing it out, I'm aware. Still `===>` would better stand out to _me_ personally. thanks, - Markus
  105197
April 10, 2019 09:27 benjamin.morel@gmail.com (Benjamin Morel)
I think that the RFC covers a great deal of possible syntaxes and their
tradeoffs.

`==>` requires *a lot* of changes to the current parser, and external
tooling as mentioned by Rowan.

It has not even been specified whether the `==>` syntax could land into PHP
7.4, or could require postponing to PHP 8.0.
I do not think that saving these two little chars `fn` justifies the extra
complexity and possible late adoption, if not by PHP, at least by external
tooling.
Also, extra parser complexity potentially means slower compilation times,
even if it's just during opcache warmup.

@nikic, the RFC does not mention changes to reflection:

- Will arrow functions be accessed through ReflectionFunction as well?
- Will they be distinguishable from regular closures, with a method such as
`isShortClosure()` or `isArrowFunction()`?

Thanks,
Ben



On Wed, 10 Apr 2019 at 11:08, Markus Fischer <markus@fischer.name> wrote:

> Hi, Gabriel, > > On 10.04.19 10:33, Gabriel O wrote: > > Those parentheses are important when having multiple argument > > Please don't top post, thanks! > > Thanks for pointing it out, I'm aware. > > Still `===>` would better stand out to _me_ personally. > > thanks, > - Markus > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > >