Re: [RFC] Arrow functions / short closures
| From: | Nikita Popov | Date: | Thu, 14 Mar 2019 15:41:33 +0000 |
| Subject: | Re: [RFC] Arrow functions / short closures | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-104718@lists.php.net to get a copy of this message | ||
On Wed, Mar 13, 2019 at 4:56 PM Nikita Popov <nikita.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
>
As a small update, I've implemented a proof of concept that uses the ($x)
==> $x * $multiplier syntax (or $x ==> $x * $multiplier for short) in
https://github.com/php/php-src/pull/3945. As
mentioned in the RFC, this
requires scanahead in the lexer.
This syntax is in principle still on the table, though personally I prefer
fn($x, $y) => $x * $y over ($x, $y) ==> $x * $y. The main redeeming quality
of ==> is that it supports the paren-less variant $x ==> $x. Taking into
account the lexer hackery it requires (and which will also be required in
any 3rd party tooling), this would not be my preferred choice.
Regards,
Nikita