# RFC: Support for multi-line arrow functions

- Thread: https://externals.io/message/111991
- Original archive: https://news-web.php.net/php.internals/111991
- Started by Nuno Maduro on 2020-10-03T21:09:38+00:00
- Messages: 24
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #111991 — Nuno Maduro — 2020-10-03T21:09:38+00:00

URL: https://externals.io/message/111991#111991

Hey internals,


A few days ago I opened a pull request that adds support for multi-line
arrow functions in PHP: https://github.com/php/php-src/pull/6246.


As you may already know, PHP 7.4 has introduced one-liner arrow functions
(aka short closures). Now, this pull request adds the possibility of those
arrow functions to be multi-line. Just like in Javascript, or Hack. Let's
see an example:


```php

$users = [/** */];

$guestsIds = [/** */];

$repository = /** */;


$guests = array_filter($users, fn ($user) => {

  $guest = $repository->findByUserId($user->id);


  return $guest !== null && in_array($guest->id, $guestsIds);

});

```


In short, the advantages are:

```

$values = array_filter($values, function ($value) use ($first, $second,
$third) {

// vs

$values = array_filter($values, fn ($value) => {

```


- Multi-line arrow functions don't require the `use` keyword to be able to
access data from the outer scope.

- Also, in some cases, `fn (/** */) => {` is just shorter and simpler than
`function (/** */) use (/** */) {`.


This pull request has been very well received by the community on Github,
and also on Twitter:
https://twitter.com/enunomaduro/status/1311572174256340992.


So, I am writing this email to get your feedback about having this feature
in the core of PHP. Note that, if eventually, you think it is a good idea
to create an RFC on this, I will need to get some RFC karma - my wiki
account is nunomaduro.


Regards,

Nuno - https://twitter.com/enunomaduro.

---

## #111992 — Marco Pivetta — 2020-10-03T21:39:32+00:00

URL: https://externals.io/message/111991#111992 · In reply to: #111991

Hey Nuno,
<http://ocramius.github.com/>




> Hey internals,
>
>
> A few days ago I opened a pull request that adds support for multi-line
> arrow functions in PHP: https://github.com/php/php-src/pull/6246.
>
>
> As you may already know, PHP 7.4 has introduced one-liner arrow functions
> (aka short closures). Now, this pull request adds the possibility of those
> arrow functions to be multi-line. Just like in Javascript, or Hack. Let's
> see an example:
>
>
> ```php
>
> $users = [/** */];
>
> $guestsIds = [/** */];
>
> $repository = /** */;
>
>
> $guests = array_filter($users, fn ($user) => {
>
>   $guest = $repository->findByUserId($user->id);
>
>
>   return $guest !== null && in_array($guest->id, $guestsIds);
>
> });
>
> ```
>
>
> In short, the advantages are:
>
> ```
>
> $values = array_filter($values, function ($value) use ($first, $second,
> $third) {
>
> // vs
>
> $values = array_filter($values, fn ($value) => {
>
> ```
>
>
> - Multi-line arrow functions don't require the `use` keyword to be able to
> access data from the outer scope.
>
> - Also, in some cases, `fn (/** */) => {` is just shorter and simpler than
> `function (/** */) use (/** */) {`.
>
>
> This pull request has been very well received by the community on Github,
> and also on Twitter:
> https://twitter.com/enunomaduro/status/1311572174256340992.
>
>
> So, I am writing this email to get your feedback about having this feature
> in the core of PHP. Note that, if eventually, you think it is a good idea
> to create an RFC on this, I will need to get some RFC karma - my wiki
> account is nunomaduro.
>

Overall, seems like unnecessary added syntax for something that already
works just fine.

Pros:

 * unclear

Cons:

 * more scope leak
 * +1 way to do what we already do just fine
 * more lexer/ast complexity for downstream projects

If you want to make an RFC about it, it really needs a compelling "pros"
section, because there aren't any, so far :-\

Greets,

Marco Pivetta

http://twitter.com/Ocramius

http://ocramius.github.com/

---

## #111993 — Benjamin Morel — 2020-10-03T22:05:13+00:00

URL: https://externals.io/message/111991#111993 · In reply to: #111992

>
> If you want to make an RFC about it, it really needs a compelling "pros"
> section, because there aren't any, so far :-\


I don't agree. There is one pro, and not a small one IMO if you use
closures a lot: skip use().

Since arrow functions have been introduced, I've several times naturally
tried to open braces, only to remember that it's not supported.
I love fn() for the fact that it doesn't require the use() statement, and
would love to see it extended to more than a simple expression.

— Benjamin

---

## #111994 — Deleu — 2020-10-03T22:08:53+00:00

URL: https://externals.io/message/111991#111994 · In reply to: #111991

Hi Nuno, thank you very much for kicking this off! The current state of
short closure is very limited and I believe it was a stepping stone to get
to this one.
This time there's no drawback of making fn a reserved keyword since it
already is.
This change will lead to better consistency and it will fill in the
expectation gap when using short closure in PHP.
Best of luck for us on this RFC.



> Hey internals,
>
>
> A few days ago I opened a pull request that adds support for multi-line
> arrow functions in PHP: https://github.com/php/php-src/pull/6246.
>
>
> As you may already know, PHP 7.4 has introduced one-liner arrow functions
> (aka short closures). Now, this pull request adds the possibility of those
> arrow functions to be multi-line. Just like in Javascript, or Hack. Let's
> see an example:
>
>
> ```php
>
> $users = [/** */];
>
> $guestsIds = [/** */];
>
> $repository = /** */;
>
>
> $guests = array_filter($users, fn ($user) => {
>
>   $guest = $repository->findByUserId($user->id);
>
>
>   return $guest !== null && in_array($guest->id, $guestsIds);
>
> });
>
> ```
>
>
> In short, the advantages are:
>
> ```
>
> $values = array_filter($values, function ($value) use ($first, $second,
> $third) {
>
> // vs
>
> $values = array_filter($values, fn ($value) => {
>
> ```
>
>
> - Multi-line arrow functions don't require the `use` keyword to be able to
> access data from the outer scope.
>
> - Also, in some cases, `fn (/** */) => {` is just shorter and simpler than
> `function (/** */) use (/** */) {`.
>
>
> This pull request has been very well received by the community on Github,
> and also on Twitter:
> https://twitter.com/enunomaduro/status/1311572174256340992.
>
>
> So, I am writing this email to get your feedback about having this feature
> in the core of PHP. Note that, if eventually, you think it is a good idea
> to create an RFC on this, I will need to get some RFC karma - my wiki
> account is nunomaduro.
>
>
> Regards,
>
> Nuno - https://twitter.com/enunomaduro.

---

## #111995 — Rowan Tommins — 2020-10-04T20:08:07+00:00

URL: https://externals.io/message/111991#111995 · In reply to: #111991

Hi Nuno,


> A few days ago I opened a pull request that adds support for multi-line
> arrow functions in PHP: https://github.com/php/php-src/pull/6246.


Welcome to the list. Firstly, it's probably worth having a look in the 
mailing list archives for prior discussions on this, as it was 
definitely discussed during the earlier short closures RFCs (the 
successful one that gave us fn()=>expr, and a couple of earlier attempts 
with different syntax and features).


Secondly, I'd like to point out that "short closures" actually have 
three fundamental features:

1. They have an implicit "return", making them ideal for single 
expressions rather than blocks of procedural code.
2. They automatically capture all variables in scope, rather than having 
to import them with "use".
3. They are shorter than normal closure declarations, both because of 
the above two features, and because "fn" is slightly shorter than 
"function".

I think it's worth making clear which of those three features we are 
hoping to retain by combining arrow functions with blocks.


Feature 1 doesn't extend to code blocks in an obvious way. In some 
languages, every statement is a valid expression, so you can place an 
implicit "return" before the last statement of a block. In PHP, that's 
not the case, so e.g. { $x = 'Hello World; echo $x; } cannot be 
converted to { $x = 'Hello World; return echo $x; }. Alternatives 
include converting to { $x = 'Hello World; echo $x; return null; } or 
requiring all closure blocks to end with a valid expression.


Feature 2 is probably the one most people actually want extended, but in 
my opinion is also the part most in need of justification, because it 
changes the language quite fundamentally.

There are currently very few places in PHP where the scope of a variable 
is not to the current function: properties of the current object must be 
accessed via $this, and class properties via self:: or similar; globals 
must be imported via a "global" statement, statics via a "static" 
statement, and closed-over values via the "use" keyword. (The main 
exception to this rule is the half-dozen built-in "superglobals"; I 
think there are a few more obscure cases.)

In a single-expression closure, as currently allowed, there is limited 
possibility for ambiguous scope. Extending this to function bodies of 
any size leads to much more risk of complexity and confusion.

If you want to capture variables $a, $b, and $c, but have local 
variables $x, $y, and $z, you would currently write this:

$f = function() use ($a, $b, $c) {
     // $x, $y, $z must be local, because not imported
}

If we added an opt-in syntax for "capture everything", we might instead 
write this:

$f = function() use (*) {
      $x = $y = $z = null;
}

Without re-initialising all local variables, we would no longer be able 
to know if they were actually local without looking at the surrounding 
scope for a value that might be captured. I am unconvinced by this 
trade-off of opt-out instead of opt-in.

One use case I've seen proposed is closures which capture a large number 
of variables; I would be interested to see an example where this is the 
case and is not a "code smell" in the same way as requiring a large 
number of parameters.


In the above example I deliberately did not use the "fn()=>" syntax, 
because I believe this is really orthogonal to the other features - my 
impression is that actual expression length (feature 3 above) is more of 
a pleasant side-effect than a top priority for most people.

I would personally prefer the "fn()=>" syntax to carry on meaning "this 
is an expression elevated to function status", and have some other 
syntax for a full closure that uses auto-capturing scope rules if that 
feature is indeed needed.


Regards,

-- 
Rowan Tommins (né Collins)
[IMSoP]

---

## #111999 — Mike Schinkel — 2020-10-05T00:41:41+00:00

URL: https://externals.io/message/111991#111999 · In reply to: #111995

> 
> 
> Hi Nuno,
> 
> 
> > […]
> 
> 
> Welcome to the list. Firstly, it's probably worth having a look in the mailing list archives for prior discussions on this, as it was definitely discussed during the earlier short closures RFCs (the successful one that gave us fn()=>expr, and a couple of earlier attempts with different syntax and features).
> 
> 
> Secondly, I'd like to point out that "short closures" actually have three fundamental features:
> 
> 1. They have an implicit "return", making them ideal for single expressions rather than blocks of procedural code.
> 2. They automatically capture all variables in scope, rather than having to import them with "use".
> 3. They are shorter than normal closure declarations, both because of the above two features, and because "fn" is slightly shorter than "function".
> 
> I think it's worth making clear which of those three features we are hoping to retain by combining arrow functions with blocks.
> 
> 
> Feature 1 doesn't extend to code blocks in an obvious way. In some languages, every statement is a valid expression, so you can place an implicit "return" before the last statement of a block. In PHP, that's not the case, so e.g. { $x = 'Hello World; echo $x; } cannot be converted to { $x = 'Hello World; return echo $x; }. Alternatives include converting to { $x = 'Hello World; echo $x; return null; } or requiring all closure blocks to end with a valid expression.
> 
> 
> Feature 2 is probably the one most people actually want extended, but in my opinion is also the part most in need of justification, because it changes the language quite fundamentally.
> 
> There are currently very few places in PHP where the scope of a variable is not to the current function: properties of the current object must be accessed via $this, and class properties via self:: or similar; globals must be imported via a "global" statement, statics via a "static" statement, and closed-over values via the "use" keyword. (The main exception to this rule is the half-dozen built-in "superglobals"; I think there are a few more obscure cases.)
> 
> In a single-expression closure, as currently allowed, there is limited possibility for ambiguous scope. Extending this to function bodies of any size leads to much more risk of complexity and confusion.
> 
> If you want to capture variables $a, $b, and $c, but have local variables $x, $y, and $z, you would currently write this:
> 
> $f = function() use ($a, $b, $c) {
>     // $x, $y, $z must be local, because not imported
> }
> 
> If we added an opt-in syntax for "capture everything", we might instead write this:
> 
> $f = function() use (*) {
>      $x = $y = $z = null;
> }
> 
> Without re-initialising all local variables, we would no longer be able to know if they were actually local without looking at the surrounding scope for a value that might be captured. I am unconvinced by this trade-off of opt-out instead of opt-in.
> 
> One use case I've seen proposed is closures which capture a large number of variables; I would be interested to see an example where this is the case and is not a "code smell" in the same way as requiring a large number of parameters.
> 
> In the above example I deliberately did not use the "fn()=>" syntax, because I believe this is really orthogonal to the other features - my impression is that actual expression length (feature 3 above) is more of a pleasant side-effect than a top priority for most people.
> 
> I would personally prefer the "fn()=>" syntax to carry on meaning "this is an expression elevated to function status", and have some other syntax for a full closure that uses auto-capturing scope rules if that feature is indeed needed.

Good analysis.

I agree #2 is the strongest motivator for some kind of improvement / change.  

In at least one prior language I have used there was no distinction between local scope inside and outside of a closure, so when I first realized I had to use the "use" clause to make variable visible it was surprising to me.

In PHP I find this requirement rather annoying in the majority of cases. My functions and closures tend to be short and the number of local variables I use — both inside and outside the closure —tend to be small so I don't have a problem with confusing scope. Having to explicitly name the variables in a use statement feels like unfortunate overkill.  But I do get your point.  

OTOH, when inherited local variables are NOT modified within the closure, does that actually cause a problem? (honest question)

If not — and please check my logic — I would suggest PHP 8.1+ could allow closures to IMPLICITLY inherit local variables, but ONLY for variables that are READ, not for variables that are written.

Given this approach, variables would still need to be declared when variables need to be written, e.g. "use ( &$foo )"

Although this relaxing of requirement to declare variables could theoretically break existing code, only code where variables are read inside closures before they are assigned with the same name as variables assigned outside the closure would "break", and those cases are flagged as a warning when error reporting is on.

For me, that would probably cover 90%+ of the use cases my the requirement to employ the "use" statement feels like overkill.

-Mike

---

## #112000 — Andreas Leathley — 2020-10-05T09:57:31+00:00

URL: https://externals.io/message/111991#112000 · In reply to: #111995

> If we added an opt-in syntax for "capture everything", we might
> instead write this:
>
> $f = function() use (*) {
>      $x = $y = $z = null;
> }
>
> Without re-initialising all local variables, we would no longer be
> able to know if they were actually local without looking at the
> surrounding scope for a value that might be captured. I am unconvinced
> by this trade-off of opt-out instead of opt-in.
>
> One use case I've seen proposed is closures which capture a large
> number of variables; I would be interested to see an example where
> this is the case and is not a "code smell" in the same way as
> requiring a large number of parameters.

Something like "use (*)" seems like a great enhancement to me. I often
use a wrapper function for SQL transactions, something like:

```php
public function update(int $numberId, int $addressId, bool $isMainNumber
= false): void
{
     $this->transaction->run(function () use ($numberId, $addressId,
$isMainNumber): void {
       // Do all SQL queries for the update
     });
}

```

In these cases there is a lot of redundancy because of having to import
the variables, and if a variable is added, it has to be added in two
places in a slightly different way. The following would be much nicer:

```php
public function update(int $numberId, int $addressId, bool $isMainNumber
= false): void
{
     $this->transaction->run(function () use (*): void {
       // Do all SQL queries for the update
     });
}

```

This would also increase code readability.

---

## #112001 — vorismi3@fel.cvut.cz — 2020-10-05T10:00:26+00:00

URL: https://externals.io/message/111991#112001 · In reply to: #112000

Yes, "use (*)" is perfect!

With kind regards / Mit freundlichen Grüßen / S přátelským pozdravem,

Michael Voříšek



> On 04.10.20 22:08, Rowan Tommins wrote: 
> 
> > […]
> 
> Something like "use (*)" seems like a great enhancement to me. I often
> use a wrapper function for SQL transactions, something like:
> 
> ```php
> public function update(int $numberId, int $addressId, bool $isMainNumber
> = false): void
> {
> $this->transaction->run(function () use ($numberId, $addressId,
> $isMainNumber): void {
> // Do all SQL queries for the update
> });
> }
> 
> ```
> 
> In these cases there is a lot of redundancy because of having to import
> the variables, and if a variable is added, it has to be added in two
> places in a slightly different way. The following would be much nicer:
> 
> ```php
> public function update(int $numberId, int $addressId, bool $isMainNumber
> = false): void
> {
> $this->transaction->run(function () use (*): void {
> // Do all SQL queries for the update
> });
> }
> 
> ```
> 
> This would also increase code readability.
> 
> --
> 
> To unsubscribe, visit: https://www.php.net/unsub.php

---

## #112002 — Lynn — 2020-10-05T10:08:26+00:00

URL: https://externals.io/message/111991#112002 · In reply to: #112001

On Mon, Oct 5, 2020 at 12:00 PM Michael Voříšek - ČVUT FEL <
vorismi3@fel.cvut.cz> wrote:

> Yes, "use (*)" is perfect!
>
> With kind regards / Mit freundlichen Grüßen / S přátelským pozdravem,
>
> Michael Voříšek
>
> 
>
> > […]


How should php deal with the scenario where you want to `use` everything
and have one variable by reference?

```
function () use (*, &$butNotThisOne) {};
```

---

## #112003 — Deleu — 2020-10-05T10:15:27+00:00

URL: https://externals.io/message/111991#112003 · In reply to: #112000

To me that seems like a great argument in favour of the proposal. If you'll
want all variables to be imported (which in this case makes completely
sense), then `fn() {}` or `fn() => {}` is much less verbose and inline with
the mentality to reach for short closures. We reach for short closures to
avoid `use()` and convey that the outer process is intertwined with the
inner process. `fn()` allows to strengthen the concept that there's no real
separation between running SQL stuff in a `callable` that wraps a database
transaction.



> 
> > […]
>
> Something like "use (*)" seems like a great enhancement to me. I often
> use a wrapper function for SQL transactions, something like:
>
> ```php
> public function update(int $numberId, int $addressId, bool $isMainNumber
> = false): void
> {
>      $this->transaction->run(function () use ($numberId, $addressId,
> $isMainNumber): void {
>        // Do all SQL queries for the update
>      });
> }
>
> ```
>
> In these cases there is a lot of redundancy because of having to import
> the variables, and if a variable is added, it has to be added in two
> places in a slightly different way. The following would be much nicer:
>
> ```php
> public function update(int $numberId, int $addressId, bool $isMainNumber
> = false): void
> {
>      $this->transaction->run(function () use (*): void {
>        // Do all SQL queries for the update
>      });
> }
>
> ```
>
> This would also increase code readability.
>
> --
> 
> To unsubscribe, visit: https://www.php.net/unsub.php
>
>

-- 
Marco Aurélio Deleu

---

## #112004 — Andreas Leathley — 2020-10-05T10:24:44+00:00

URL: https://externals.io/message/111991#112004 · In reply to: #112002

> How should php deal with the scenario where you want to `use` everything
> and have one variable by reference?
>
> ```
> function () use (*, &$butNotThisOne) {};
> ```

The easiest would be to only allow "use (*)" with no references or
additional syntax. "use (*)" would only copy all local variables into
the closure, no references. Personally I have never used references with
"use", I think it is much more niche compared to the regular copying,
and there is still the explicit (current) syntax to do references.

---

## #112005 — Andreas Leathley — 2020-10-05T10:39:57+00:00

URL: https://externals.io/message/111991#112005 · In reply to: #112003

> To me that seems like a great argument in favour of the proposal. If you'll
> want all variables to be imported (which in this case makes completely
> sense), then `fn() {}` or `fn() => {}` is much less verbose and inline with
> the mentality to reach for short closures. We reach for short closures to
> avoid `use()` and convey that the outer process is intertwined with the
> inner process. `fn()` allows to strengthen the concept that there's no real
> separation between running SQL stuff in a `callable` that wraps a database
> transaction.

Not necessarily: the arrow functions were specifically implemented for
very short anonymous functions with a return value. Making them more and
more like the existing "function" syntax would lead to having two
different ways of defining anonymous functions that mainly differentiate
themselves by including the parent scope by default or not.

I like the "function () use (*)" suggestion because it is explicit and
opt-in. A shorter syntax like "fn () {}" is less clear, and it could
lead to many people always using fn just because it is faster to write
(and less to think about), which then could lead to unintended side
effects because variables are being copied from the parent scope each
time. When you see a usage of "fn () {}" while reading code you would
not know if the person used it because it was faster to write, or if the
parent scope variables are really needed.

---

## #112006 — Markus Fischer — 2020-10-05T13:37:16+00:00

URL: https://externals.io/message/111991#112006 · In reply to: #112004

> 
> > […]
> 
> The easiest would be to only allow "use (*)" with no references or
> additional syntax. "use (*)" would only copy all local variables into
> the closure, no references. Personally I have never used references with
> "use", I think it is much more niche compared to the regular copying,
> and there is still the explicit (current) syntax to do references.

FTR, short arrow function implicitly only support "by value" bindings, I 
just checked the docs.

I do use by ref with closures btw.

- Markus

---

## #112010 — Brent Roose — 2020-10-06T08:53:04+00:00

URL: https://externals.io/message/111991#112010 · In reply to: #112005

Hi internals

The reason multi-line short closures are so valued by userland devs is because they are shorter to write and prettier to read. While some of us might not agree on the definition of "prettier to read", it was one of the key arguments for adding short closures in the first place:

> Anonymous functions in PHP can be quite verbose, even when they only perform a simple operation. Partly this is due to a large amount of syntactic boilerplate, and partly due to the need to manually import used variables. This makes code using simple closures hard to read and understand. This RFC proposes a more concise syntax for this pattern. [1]
> 

We can have the discussion again on whether we like short closures or not, but it turned out most of internals _and_ userland devs do — based on the vote count in the sigle line RFC and the reaction on Nuno's PR, as well as my experience from an OSS maintainer point of view.

Furthermore, the `use(*)` syntax misses the point of this proposal: it's not about being able to use all variables from the outer scope, it's about a clean syntax that's as short as possible — even when you personally disagree that it is. I've made the same argument before on this list: it's clear that the PHP community _wants_ these changes: named arguments, property promotions, short closures, … these are all features that aren't _necessary_, still they are great features of a modern-day language. 

I also want to quote from Larry Garfields book on thinking functionally in PHP [2], to demonstrate the signicant impact short closures already had today:

> “Combined with PHP’s overall clunky syntax for doing functional-esque code, I generally didn’t go further than “pure functions are your friend,” either in my own code or what I explained to others.
> 
> That is, until PHP 7.4.
> 
> PHP 7.4’s introduction of short lambdas is, as we’ll see in this book, a game-changer. While it doesn’t make anything new possible, it makes a lot of things suddenly practical. That makes all the difference, so I decided it was time to buckle down and really dig into functional programming.”

Larry continues to write a whole book about functional programming in PHP, and short closures play a significant role. 

So I hope to see more input on Nuno's PR from a techinical point of view: what's missing, what's needed to get this to the RFC phase, … and not only discussions about what syntax we like or not, or whether there are other ways to solve the same problem. Please provide Nuno with actionable feedback. 

Kind regards
Brent

[1] https://wiki.php.net/rfc/arrow_functions_v2 <https://wiki.php.net/rfc/arrow_functions_v2> 
[2] https://leanpub.com/thinking-functionally-in-php <https://leanpub.com/thinking-functionally-in-php> 




> 
> 
> 
> > […]
> 
> Not necessarily: the arrow functions were specifically implemented for
> very short anonymous functions with a return value. Making them more and
> more like the existing "function" syntax would lead to having two
> different ways of defining anonymous functions that mainly differentiate
> themselves by including the parent scope by default or not.
> 
> I like the "function () use (*)" suggestion because it is explicit and
> opt-in. A shorter syntax like "fn () {}" is less clear, and it could
> lead to many people always using fn just because it is faster to write
> (and less to think about), which then could lead to unintended side
> effects because variables are being copied from the parent scope each
> time. When you see a usage of "fn () {}" while reading code you would
> not know if the person used it because it was faster to write, or if the
> parent scope variables are really needed.
> 
> --
> 
> To unsubscribe, visit: https://www.php.net/unsub.php

---

## #112011 — G. P. B. — 2020-10-06T10:28:10+00:00

URL: https://externals.io/message/111991#112011 · In reply to: #112010

First, can you please bottom-post and not top-post.



> Hi internals
>
> The reason multi-line short closures are so valued by userland devs is
> because they are shorter to write and prettier to read. While some of us
> might not agree on the definition of "prettier to read", it was one of the
> key arguments for adding short closures in the first place:
>
> > […]
> perform a simple operation. Partly this is due to a large amount of
> syntactic boilerplate, and partly due to the need to manually import used
> variables. This makes code using simple closures hard to read and
> understand. This RFC proposes a more concise syntax for this pattern. [1]
> > […]
>
> We can have the discussion again on whether we like short closures or not,
> but it turned out most of internals _and_ userland devs do — based on the
> vote count in the sigle line RFC and the reaction on Nuno's PR, as well as
> my experience from an OSS maintainer point of view.
>

I didn't know we were meant to do code golfing with production code, might
have missed a memo somewhere.


> Furthermore, the `use(*)` syntax misses the point of this proposal: it's
> not about being able to use all variables from the outer scope,


If it's not about being able to use all variables (or even just one that is
irrelevant) from the outer scope, then what is the point?
Saving 6 characters by only writing fn() {} instead of function {}?


> it's about a clean syntax that's as short as possible — even when you
> personally disagree that it is. I've made the same argument before on this
> list: it's clear that the PHP community _wants_ these changes:


Wanting something isn't an argument. Looking at what part of the community
wants, we should be using @ for attributes.

named arguments, property promotions, short closures, … these are all
> features that aren't _necessary_, still they are great features of a
> modern-day language.
>

Obviously nothing is necessary, we could write assembler style with only
goto statements.

I also want to quote from Larry Garfields book on thinking functionally in
> PHP [2], to demonstrate the signicant impact short closures already had
> today:
>
> > […]
> code, I generally didn’t go further than “pure functions are your friend,”
> either in my own code or what I explained to others.
> > […]
> game-changer. While it doesn’t make anything new possible, it makes a lot
> of things suddenly practical. That makes all the difference, so I decided
> it was time to buckle down and really dig into functional programming.”
>
> Larry continues to write a whole book about functional programming in PHP,
> and short closures play a significant role.
>

Finally a resemblance of an actual argument.


> So I hope to see more input on Nuno's PR from a techinical point of view:
> what's missing, what's needed to get this to the RFC phase, … and not only
> discussions about what syntax we like or not, or whether there are other
> ways to solve the same problem. Please provide Nuno with actionable
> feedback.
>
> Kind regards
> Brent
>
> [1] https://wiki.php.net/rfc/arrow_functions_v2 <
> https://wiki.php.net/rfc/arrow_functions_v2>
> [2] https://leanpub.com/thinking-functionally-in-php <
> https://leanpub.com/thinking-functionally-in-php>
>

Jokes aside, the actionable feedback is to argue *why* auto capture of the
outer scope should be added to the
language as "it is very not PHP" a direct quote from Rasmus from his talk
"25 years of PHP" [1] and from the
same section one of the reasons why people don't mind the current single
line expression form is because it
doesn't look like a new scope.
As Rowan said in his analysis changing this specific behaviour of scope
being able to "leak" into another one
needs a lot of justification, the current short closure syntax doesn't even
use braces {} which are the de facto
signal in PHP that you are entering in a new scope.

Going back to the `use(*)` syntax: the reason why people propose this
extension (which is not mutually exclusive
with adding support for fn {} without outer scope capture, albeit strange)
is that it is more in PHP traditional design
philosophy. You can argue against this syntax and in favour of Nuno's, but
again it is NOT missing the point.

Moreover, Larry has also made a PR which extends short closures [2] in a
way I personally find way more appealing.

Regards

George P. Banyard

[1] https://youtu.be/Qa_xVjTiOUw?t=1895
[2] https://github.com/php/php-src/pull/6221

---

## #112012 — G. P. B. — 2020-10-06T10:29:29+00:00

URL: https://externals.io/message/111991#112012 · In reply to: #112011

> First, can you please bottom-post and not top-post.
>
> 
>
> > […]
>
> I didn't know we were meant to do code golfing with production code, might
> have missed a memo somewhere.
>
>
> > […]
>
>
> If it's not about being able to use all variables (or even just one that
> is irrelevant) from the outer scope, then what is the point?
> Saving 6 characters by only writing fn() {} instead of function {}?
>
>
> > […]
>
>
> Wanting something isn't an argument. Looking at what part of the community
> wants, we should be using @ for attributes.
>
> named arguments, property promotions, short closures, … these are all
> > […]
>
> Obviously nothing is necessary, we could write assembler style with only
> goto statements.
>
> I also want to quote from Larry Garfields book on thinking functionally in
> > […]
>
> Finally a resemblance of an actual argument.
>
>
> > […]
>
> Jokes aside, the actionable feedback is to argue *why* auto capture of the
> outer scope should be added to the
> language as "it is very not PHP" a direct quote from Rasmus from his talk
> "25 years of PHP" [1] and from the
> same section one of the reasons why people don't mind the current single
> line expression form is because it
> doesn't look like a new scope.
> As Rowan said in his analysis changing this specific behaviour of scope
> being able to "leak" into another one
> needs a lot of justification, the current short closure syntax doesn't
> even use braces {} which are the de facto
> signal in PHP that you are entering in a new scope.
>
> Going back to the `use(*)` syntax: the reason why people propose this
> extension (which is not mutually exclusive
> with adding support for fn {} without outer scope capture, albeit strange)
> is that it is more in PHP traditional design
> philosophy. You can argue against this syntax and in favour of Nuno's, but
> again it is NOT missing the point.
>
> Moreover, Larry has also made a PR which extends short closures [2] in a
> way I personally find way more appealing.
>
> Regards
>
> George P. Banyard
>
> [1] https://youtu.be/Qa_xVjTiOUw?t=1895
> [2] https://github.com/php/php-src/pull/6221
>

Ressending to the list as I seem to have messed up my reply.

---

## #112013 — G. P. B. — 2020-10-06T12:09:01+00:00

URL: https://externals.io/message/111991#112013

> The point of short closures, regardless of single line or multi line, is
> addressed (and agreed upon by the RFC votes) in the first pararaph of the
> RFC [1]. I'm not sure if I can add anything useful rather than saying "it's
> nice to be able to write more consise code. It feels a little more smooth".
>

The thing is, the RFC voters have agreed on single-*expression*, there is a
high likelihood that if multi-line
(i.e. multiple statements/expression) support was included in this RFC it
would have failed.
Why do I think this? Look at the previous vote:
https://wiki.php.net/rfc/short_closures
So your conclusion about what the voters agreed is completely nonsensical
here.


> There's actually one more argument to be made: they would make the use of
> closures more consistent for those of who _prefer_ short closures. Now
> you're forced to mix up both the short and long syntax. As an example, I
> use Laravel's collections [2] which provide a functional API to interact
> with lists of data. You often chain these calls, and are able to use short
> closures like 80% of the time, but sometimes you're forced to use the
> longer syntax.
>
> So that's my two arguments for multi-line closures
>
> - Making code less verbose, which is the _same_ goal of single-line short
> closures, which was accepted by the vast majority of PHP
> - Consistency with already existing single-line short closures
>

Ignoring the first point, refer back to the previous paragraph.
What consistency is there if there is no implicit return in the block form?
Sure it is annoying that for a 2-liner, where you probably won't "forget"
about
the fact that you are importing the outer scope it is annoying that you
need to
go the longer route. But that's not a sufficient argument IMHO.


> My point is that PHP is evolving towards a modern language, and with that
> come syntax changes that not everyone likes or wants to use. @ for
> attributes isn't chosen because it simply isn't possible to support that
> syntax, not because of what the community _and_ internals want or don't
> want. I don't think of this as a counter argument to my original point: PHP
> has seen a significant increase in syntax sugar additions over the past few
> years. If no one wanted those, they woulnd't have been voted in. Would you
> like to address that argument? Why are promoted properties, single-line
> short closures, named argument, etc added besides that there's a "want" for
> them?
>

The core difference here is that it doesn't change a *fundamental* aspect
of the language
(or in the case of single-expression closure it changes 3 aspects making
them very
special), you might not like this aspect but it is still present.


Obviously nothing is necessary, we could write assembler style with only
goto statements.

That's exactly what I'm saying! It's ok to add syntax to make a developer's
> life easier. There are cases where that's a good enough argument.
>

And this is not one of them, at least for me, and I don't think I'm alone
here.


> Why was auto capture added to single line short closures last year? We've
> had this discussion before, and the majority of internals voted "yes" back
> then. So instead of only re-iterating the same discussion, why not provide
> Nuno with useful feedback as well?
>

And once *again* short-closure don't *just* have the auto-import of the
outer scope going for it.
The other main features of the short closure are not applicable to a block
syntax.
 - the fact that there is an implicit return
 - it is a single expression, *no* braces therefore it doesn't look like it
creates a new scope.

You ignoring these facets which are key benefits which only apply to the
short closure case, to imply
the extension to multi-line is a "no-brainer" is not making a case for it.

My opinion is already that the way PHP does closures is the Sane Way To Do
It TM,
you have a dependency on a variable therefore you need to specify it, is
IMHO a good thing.

Do other languages function just fine with the opposite behaviour, sure. At
the same time Java
has implicit nullable type arguments and people deal with it.


Best regards

George P. Banyard

---

## #112022 — Rowan Tommins — 2020-10-07T19:26:52+00:00

URL: https://externals.io/message/111991#112022 · In reply to: #112010

Hi Brent,


> We can have the discussion again on whether we like short closures or not, but it turned out most of internals_and_  userland devs do — based on the vote count in the sigle line RFC and the reaction on Nuno's PR, as well as my experience from an OSS maintainer point of view.


I don't know how much of the earlier discussion you saw at the time, or 
have looked back at, but it's worth noting that the arrow functions RFC 
which passed voting came after several long mailing list threads, and 
several complete rewrites of the proposal, at least one of which went to 
vote and was declined. The lack of multi-statement arrow functions 
wasn't an accident, it was part of how consensus was achieved.

That doesn't mean we can't discuss multi-statement arrow functions now - 
they were listed as "Future Scope", and we are now in that Future. :) 
However, it does mean a new RFC doing so needs to set out its own 
justification, not assume that everyone who voted Yes on the previous 
RFC will vote Yes on this one as well.


> Furthermore, the `use(*)` syntax misses the point of this proposal: it's not about being able to use all variables from the outer scope, it's about a clean syntax that's as short as possible


Rather than "missing the point", I think it's a case of different people 
wanting different things from arrow functions. In one of the previous 
discussions [1] I suggested three perspectives of what people wanted 
them for:

(a) an improved syntax for declaring closures (in the way that [$foo, 
$bar] is a shorter syntax for array($foo, $bar))
(b) improved *semantics* for declaring closures, where you don't have to 
list the variables to be captured
(c) a new kind of closure designed for specific use cases, with its own 
syntax and semantics

Those who wanted (c) are generally happy with what we have, with perhaps 
an interest in making more statements available as expressions, such as 
throw [2] and match [3].

Those who wanted (b) generally see the arrow syntax as a means to an 
end, and are open to alternative syntaxes that achieve the relevant 
semantics, such as use(*).

Those who wanted (a) may see things the other way around: the short 
syntax is the goal, and auto-capture and implicit return are just the 
ways to achieve that goal.

There's probably no way to please all three groups (and the various 
combinations and nuances I've over-simplified), but it's useful to 
acknowledge the existence of those who want *some* of what you want, but 
don't agree on all of it.


I look forward to seeing the RFC if Nuno decides to proceed with it, 
because I am much more interested in how it will affect *users* of PHP 
than the technical details visible in the PR.


[1] https://externals.io/message/98045#98209
[2] https://wiki.php.net/rfc/throw_expression
[3] https://wiki.php.net/rfc/match_expression_v2


Regards,

-- 
Rowan Tommins (né Collins)
[IMSoP]

---

## #112146 — Benjamin Morel — 2020-10-29T22:49:09+00:00

URL: https://externals.io/message/111991#112146 · In reply to: #112013

> And once *again* short-closure don't *just* have the auto-import of the
> outer scope going for it.
> The other main features of the short closure are not applicable to a block
> syntax.
>  - the fact that there is an implicit return
>  - it is a single expression,
> **no* braces therefore it doesn't look like it creates a new scope.*
>

I'd like to come back to this. Actually there is a precedent in PHP where
braces don't create a new scope:

    $a = 1;

    {
        $a++;
    }

    echo $a; // 2

I'm not sure why this syntax was allowed in the first place, but it is. So
I wouldn't be bothered if we create yet another case of block not creating
a new scope.

I'd like to emphasize the use case brought by Andreas Leathley:
transactional scope. I'm doing more and more of this these days, and I find
it very appealing:

    $connection->transactional(function() {
        // send raw SQL queries
        // or create a scoped EntityManager
        // etc.
    });

This is, IMO, the only way to easily guarantee that any `return` will
commit the transaction, and any `throw` will roll it back. Otherwise, you
must remember to wrap your *whole* block with try {} and
catch/rollback/rethrow. That's a lot of clutter that you'll typically want
to avoid when you're repeating this in each and every method of every
service.

Now, the example above doesn't convey the purpose of the RFC on its own.
Let's put it into context, here is how one of my service methods looks like:

    return $connection->transactional(function() use ($authorId,
$activityId, $parentId, $comment) {

... and this is not the largest one. Let's see how it would look like with
this proposal:

    return $connection->transactional(fn() => {

This may not look like a big gain for you, but this would make all my
services look MUCH cleaner. And I'm not worried about scope leaking:
usually, the closure takes up most of my service method.

— Benjamin

---

## #112147 — Sara Golemon — 2020-10-30T02:50:50+00:00

URL: https://externals.io/message/111991#112147 · In reply to: #112002

> How should php deal with the scenario where you want to `use` everything
> and have one variable by reference?
>
> ```
> function () use (*, &$butNotThisOne) {};
> ```

I would take a page out of C++'s book here.  In C++ a closure is (some of
these bits can be omitted for brevity, but I'm not going to describe those
here as it's orthogonal to the topic):

[capture, vars](type argName) -> returnType {
  statements;
  return retVal;
}

Looking specifically at the capture vars section (in the example we are
capturing two variables, one named 'capture', one named 'vars'), there are
wildcards available as well:

[=](bool bval) -> void {}

The equal sign (as above) captures all variables used in the closure by
value.
Similarly, the ampersand `[&](int ival) -> void {}` captures all variables
used by reference.

Exceptions can be added to that list just as you suggested, so:

[=,&foo](double dval) -> double { return foo + dval; }

Or

[&,foo]() -> void { doSomething(foo); }

I think we could apply the same in PHP terms:

function(float $dval) use ($, &$foo) { return $foo + $dval; };
function() use (&$, $foo) { doSomething($foo); };

Plus or minor for parser convenience.

-Sara

---

## #112148 — Rowan Tommins — 2020-10-30T18:06:51+00:00

URL: https://externals.io/message/111991#112148 · In reply to: #112146

> 
>
> Actually there is a precedent in PHP where braces don't create a new 
> scope:
>      $a = 1;
>
>      {
>          $a++;
>      }
>
>      echo $a; // 2


This is true. Indeed, in general, scope in PHP is not to the nearest 
pair of braces - if statements and loops don't create a new scope - but 
it is, with very few exceptions, to the current function, with no 
automatic inheritance/capture from the outer scope.

The fact that arrow functions *don't* have braces makes them look less 
like a normal function, so it is less surprising that they capture from 
their surrounding scope. The fact that they are limited to a single 
expression also reinforces that they are intended for a specific use 
case, and are not intended as a replacement for all anonymous functions.



> Let's put it into context, here is how one of my service methods looks like:
>
>      return $connection->transactional(function() use ($authorId,
> $activityId, $parentId, $comment) {
>
> ... and this is not the largest one. Let's see how it would look like with
> this proposal:
>
>      return $connection->transactional(fn() => {


Just to be clear, the major gain here is not replacing the 10 characters 
"function()" with the 7 characters "fn() =>", it is eliminating the list 
of captured variables. So you would get equally clean code with a 
"capture all" syntax, such as:

     return $connection->transactional(function() use(*) {

or Sara's suggestion from earlier today:

     return $connection->transactional(function() use($) {


If I've counted right, this takes your example from 46 characters down 
to 17; but more importantly, that 17 is fixed no matter how many extra 
variables you want to capture or how long you make their names.


> And I'm not worried about scope leaking:
> usually, the closure takes up most of my service method.


To me, this illustrates exactly why we should have an explicit 
"auto-capture" syntax, rather than extending arrow functions: this is a 
form you would use in specific situations where you know it's 
appropriate, *not* a general-purpose replacement for the existing syntax.

As well as being shorter than the current syntax, arrow functions would 
remind people of JavaScript, where it's considered best practice to use 
arrow functions as much as possible, because they have *better* scoping 
- in particular, they automatically bind "this", which PHP closures 
already do.

There are certainly use cases where automatic capture leads to more 
elegant code than explicit capture; but there are also plenty of cases 
where it makes code hard to follow, and leads to subtle bugs. That's why 
I would like the two syntaxes to feel like equal options to choose between.


Regards,

-- 
Rowan Tommins (né Collins)
[IMSoP]

---

## #112152 — Benjamin Morel — 2020-10-30T21:44:41+00:00

URL: https://externals.io/message/111991#112152 · In reply to: #112148

> Just to be clear, the major gain here is not replacing the 10 characters
> "function()" with the 7 characters "fn() =>", it is eliminating the list
> of captured variables. So you would get equally clean code with a
> "capture all" syntax, such as:
>
>      return $connection->transactional(function() use(*) {
>
> or Sara's suggestion from earlier today:
>
>      return $connection->transactional(function() use($) {
>

Any of those would be a large improvement over the current situation I
agree.

That being said, for what it's worth, I've also proposed on the PR on GitHub
<https://github.com/php/php-src/pull/6246#issuecomment-719074545> an
alternative syntax for a closure that takes no parameters and inherits the
full scope:

    $connection->transactional({
        // ...
    });

So yes, this would be a closure:

    $a = {};

I'm not sure if this would be considered (additionally, not as a
replacement), but I thought it'd be worth mentioning, as it would offer an
elegant solution for methods whose body is pretty much only a call to a
method wrapping a closure.

— Benjamin

---

## #112174 — Nuno Maduro — 2020-11-03T20:52:25+00:00

URL: https://externals.io/message/111991#112174 · In reply to: #112152

Hey internals,

Thank you for the feedback on this thread so far.

I think a detailed RFC will help to clarify the proposal and reasoning
behind the chosen syntax. And after that, we can keep discussing the
proposal.

So I can move forward, do you mind giving me the necessary karma to create
RFCs? My wiki account is "nunomaduro".

— Nuno

---

## #112176 — Christoph M. Becker — 2020-11-03T22:34:01+00:00

URL: https://externals.io/message/111991#112176 · In reply to: #112174

> Hey internals,
>
> Thank you for the feedback on this thread so far.
>
> I think a detailed RFC will help to clarify the proposal and reasoning
> behind the chosen syntax. And after that, we can keep discussing the
> proposal.
>
> So I can move forward, do you mind giving me the necessary karma to create
> RFCs? My wiki account is "nunomaduro".

RFC karma granted.  Best of luck with the RFC! :)

Regards,
Christoph
