# [RFC] Short Closures 2, aka auto-capture take 3

- Thread: https://externals.io/message/117888
- Original archive: https://news-web.php.net/php.internals/117888
- Started by Larry Garfield on 2022-06-09T16:34:03+00:00
- Messages: 67
- RFC: Short Closures 2.0 (Declined) — https://externals.io/rfc/auto-capture-closure.md
- Page 1 of 3: messages 1 to 25 (25 per page, add `?page=N` to the URL)
- Next page: https://externals.io/message/117888.md?page=2
- 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

---

## #117888 — Larry Garfield — 2022-06-09T16:34:03+00:00

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

Last year, Nuno Maduro and I put together an RFC for combining the multi-line capabilities of long-closures with the auto-capture compactness of short-closures.  That RFC didn't fully go to completion due to concerns over the performance impact, which Nuno and I didn't have bandwidth to resolve.

Arnaud Le Blanc has now picked up the flag with an improved implementation that includes benchmarks showing an effectively net-zero performance impact, aka, good news as it avoids over-capturing.

The RFC has therefore been overhauled accordingly and is now ready for consideration.

https://wiki.php.net/rfc/auto-capture-closure

-- 
  Larry Garfield
  larry@garfieldtech.com

---

## #117889 — Marco Pivetta — 2022-06-09T16:46:53+00:00

URL: https://externals.io/message/117888#117889 · In reply to: #117888

Hey Larry,




> Last year, Nuno Maduro and I put together an RFC for combining the
> multi-line capabilities of long-closures with the auto-capture compactness
> of short-closures.  That RFC didn't fully go to completion due to concerns
> over the performance impact, which Nuno and I didn't have bandwidth to
> resolve.
>
> Arnaud Le Blanc has now picked up the flag with an improved implementation
> that includes benchmarks showing an effectively net-zero performance
> impact, aka, good news as it avoids over-capturing.
>
> The RFC has therefore been overhauled accordingly and is now ready for
> consideration.
>
> https://wiki.php.net/rfc/auto-capture-closure
>

Couple questions:

## nesting these functions within each other

What happens when/if we nest these functions? Take this minimal example:

```php
$a = 'hello world';

(fn () {
    (fn () {
        echo $a;
    })();
})();
```

## capturing `$this`

In the past (also present), I had to type `static fn () => ...` or `static
function () { ...` all over the place, to avoid implicitly binding `$this`
to a closure, causing hidden memory leaks.

Assuming following:

 * these new closures could capture `$this` automatically, once detected
 * these new closures can optimize away unnecessary variables that aren't
captured

Would that allow us to get rid of `static fn () {` declarations, when
creating one of these closures in an instance method context?

Greets,

Marco Pivetta

https://twitter.com/Ocramius

https://ocramius.github.io/

---

## #117890 — Arnaud Le Blanc — 2022-06-09T18:15:20+00:00

URL: https://externals.io/message/117888#117890 · In reply to: #117889

Hi,

On jeudi 9 juin 2022 18:46:53 CEST Marco Pivetta wrote: 
> ## nesting these functions within each other
> 
> What happens when/if we nest these functions? Take this minimal example:
> 
> ```php
> $a = 'hello world';
> 
> (fn () {
>     (fn () {
>         echo $a;
>     })();
> })();
> ```

Capture bubbles up. When an inner function uses a variable, the outer function 
in fact uses it too, so it's captured by both functions, by-value.

This example prints "hello world": The inner function captures $a from the 
outer function, which captures $a from its declaring scope.

This is equivalent to

```php
(function () use ($a) {
    (function () use ($a) {
        echo $a;
    })();
})();
```

> ## capturing `$this`
> 
> In the past (also present), I had to type `static fn () => ...` or `static
> function () { ...` all over the place, to avoid implicitly binding `$this`
> to a closure, causing hidden memory leaks.
> 
> Assuming following:
> 
>  * these new closures could capture `$this` automatically, once detected
>  * these new closures can optimize away unnecessary variables that aren't
> captured
> 
> Would that allow us to get rid of `static fn () {` declarations, when
> creating one of these closures in an instance method context?

It would be great to get rid of this, but ideally this would apply to Arrow 
Functions and Anonymous Functions as well. This could be a separate RFC.

--
Arnaud Le Blanc

---

## #117891 — michal.brzuchalski@gmail.com — 2022-06-09T18:36:23+00:00

URL: https://externals.io/message/117888#117891 · In reply to: #117888

Hi Larry,

czw., 9 cze 2022 o 18:34 Larry Garfield <larry@garfieldtech.com> napisał(a):

> Last year, Nuno Maduro and I put together an RFC for combining the
> multi-line capabilities of long-closures with the auto-capture compactness
> of short-closures.  That RFC didn't fully go to completion due to concerns
> over the performance impact, which Nuno and I didn't have bandwidth to
> resolve.
>
> Arnaud Le Blanc has now picked up the flag with an improved implementation
> that includes benchmarks showing an effectively net-zero performance
> impact, aka, good news as it avoids over-capturing.
>
> The RFC has therefore been overhauled accordingly and is now ready for
> consideration.
>
> https://wiki.php.net/rfc/auto-capture-closure


Nice work. Well-described behaviors.

One question, more around future scope or related functionality in the
future:
A future RFC for "short-methods" described here in one of your declined RFC
https://wiki.php.net/rfc/short-functions in the past could be revived with
no conflicts in the scope of methods?

class Foo {
    public string $firstName = 'John';
    public function getFirstName(): string => $this->firstName;
}

I'm asking if I understand the scopes of this and previous RFCs correctly
and if they don't block in future "short-methods"?

Cheers,
Michał Marcin Brzuchalski

---

## #117892 — Larry Garfield — 2022-06-09T19:19:05+00:00

URL: https://externals.io/message/117888#117892 · In reply to: #117891

> Hi Larry,
>
> czw., 9 cze 2022 o 18:34 Larry Garfield <larry@garfieldtech.com> napisał(a):
>
> > […]
>
>
> Nice work. Well-described behaviors.
>
> One question, more around future scope or related functionality in the
> future:
> A future RFC for "short-methods" described here in one of your declined RFC
> https://wiki.php.net/rfc/short-functions in the past could be revived with
> no conflicts in the scope of methods?
>
> class Foo {
>     public string $firstName = 'John';
>     public function getFirstName(): string => $this->firstName;
> }
>
> I'm asking if I understand the scopes of this and previous RFCs correctly
> and if they don't block in future "short-methods"?
>
> Cheers,
> Michał Marcin Brzuchalski

The short-functions RFC is entirely separate.  The syntax choices in both that RFC and this one were made to ensure that they don't conflict with each other, and the resulting syntax meaning is consistent across the language.  The implementations are independent and should in no way conflict.

(The short-functions RFC would have enabled short-methods too.  It was purely a syntax sugar with no additional behavior.)

That's assuming the attitude toward the short-function RFC ever changes enough in the future to make it worth trying again...

--Larry Garfield

---

## #117893 — Nikita Popov — 2022-06-09T19:27:14+00:00

URL: https://externals.io/message/117888#117893 · In reply to: #117890

> Hi,
>
> On jeudi 9 juin 2022 18:46:53 CEST Marco Pivetta wrote:
> > […]
>
> Capture bubbles up. When an inner function uses a variable, the outer
> function
> in fact uses it too, so it's captured by both functions, by-value.
>
> This example prints "hello world": The inner function captures $a from the
> outer function, which captures $a from its declaring scope.
>
> This is equivalent to
>
> ```php
> (function () use ($a) {
>     (function () use ($a) {
>         echo $a;
>     })();
> })();
> ```
>
> > […]
> `static
> > […]
> `$this`
> > […]
>
> It would be great to get rid of this, but ideally this would apply to
> Arrow
> Functions and Anonymous Functions as well. This could be a separate RFC.
>

I've tried this in the past, and this is not possible due to implicit $this
uses. See
https://wiki.php.net/rfc/arrow_functions_v2#this_binding_and_static_arrow_functions
for a brief note on this. The tl;dr is that if your closure does "fn() =>
Foo::bar()" and Foo happens to be a parent of your current scope and bar()
a non-static method, then this performs a scoped instance call that
inherits $this. Not binding $this here would result in an Error exception,
but the compiler doesn't have any way to know that $this needs to be bound.

Regards,
Nikita

---

## #117894 — Marco Pivetta — 2022-06-09T19:28:59+00:00

URL: https://externals.io/message/117888#117894 · In reply to: #117893

> On Thu, Jun 9, 2022 at 8:15 PM Arnaud Le Blanc <arnaud.lb@gmail.com>
> wrote:
>
> > […]
>
> I've tried this in the past, and this is not possible due to implicit
> $this uses. See
> https://wiki.php.net/rfc/arrow_functions_v2#this_binding_and_static_arrow_functions
> for a brief note on this. The tl;dr is that if your closure does "fn() =>
> Foo::bar()" and Foo happens to be a parent of your current scope and bar()
> a non-static method, then this performs a scoped instance call that
> inherits $this. Not binding $this here would result in an Error exception,
> but the compiler doesn't have any way to know that $this needs to be bound.
>
> Regards,
> Nikita
>

Hey Nikita,

Do you have another example? Calling instance methods statically is...
well... deserving a hard crash :|

Marco Pivetta

https://twitter.com/Ocramius

https://ocramius.github.io/

---

## #117895 — Nikita Popov — 2022-06-09T19:35:37+00:00

URL: https://externals.io/message/117888#117895 · In reply to: #117894

>
> 
>
> > […]
>
> Hey Nikita,
>
> Do you have another example? Calling instance methods statically is...
> well... deserving a hard crash :|
>

Maybe easier to understand if you replace Foo::bar() with parent::bar()?
That's the most common spelling for this type of call.

I agree that the syntax we use for this is unfortunate (because it is
syntactically indistinguishable from a static method call, which it is
*not*), but that's what we have right now, and we can hardly just stop
supporting it.

Regards,
Nikita

---

## #117896 — Marco Pivetta — 2022-06-09T19:39:20+00:00

URL: https://externals.io/message/117888#117896 · In reply to: #117895

Hey Nikita,



> 
>
> > […]
>
> Maybe easier to understand if you replace Foo::bar() with parent::bar()?
> That's the most common spelling for this type of call.
>
> I agree that the syntax we use for this is unfortunate (because it is
> syntactically indistinguishable from a static method call, which it is
> *not*), but that's what we have right now, and we can hardly just stop
> supporting it.
>

Dunno, it's a new construct, so perhaps we could do something about it.
I'm not suggesting we change the existing `fn` or `function` declarations,
but in this case, we're introducing a new construct, and some work already
went in to do the eager discovery of by-val variables.

Heck, variable variables already wouldn't work here, according to this RFC
:D

Marco Pivetta

https://twitter.com/Ocramius

https://ocramius.github.io/

---

## #117897 — Nikita Popov — 2022-06-09T19:42:18+00:00

URL: https://externals.io/message/117888#117897 · In reply to: #117896

> Hey Nikita,
>
> 
>
> > […]
>
> Dunno, it's a new construct, so perhaps we could do something about it.
> I'm not suggesting we change the existing `fn` or `function` declarations,
> but in this case, we're introducing a new construct, and some work already
> went in to do the eager discovery of by-val variables.
>
> Heck, variable variables already wouldn't work here, according to this RFC
> :D
>

We're not introducing a new construct. We're just extending existing fn()
functions to accept {} blocks, with exactly the same semantics as before. I
would find it highly concerning if fn() => X and fn() => { return X; } had
differences in capture semantics. Those two expressions should be strictly
identical -- the former should be desugared to the latter.

Nikita

---

## #117905 — Rowan Tommins — 2022-06-11T21:14:28+00:00

URL: https://externals.io/message/117888#117905 · In reply to: #117888

> Last year, Nuno Maduro and I put together an RFC for combining the multi-line capabilities of long-closures with the auto-capture compactness of short-closures ... Arnaud Le Blanc has now picked up the flag with an improved implementation ... The RFC has therefore been overhauled accordingly and is now ready for consideration.
>
> https://wiki.php.net/rfc/auto-capture-closure


First of all, thanks to all three of you for the work on this. Although 
I'm not quite convinced yet, I know a lot of people have expressed 
desire for this feature over the years.


My main concern is summed up accidentally by your choice of subject line 
for this thread: is the proposal to add *short closure syntax* or is it 
to add *auto-capturing closures*?

They may sound like the same thing, but to me "short closure syntax" 
(and a lot of the current RFC) implies that the new syntax is better for 
nearly all closures, and that once it is introduced, the old syntax 
would only really be there for compatibility - similar to how the [] 
syntax replaces array() and list(). If that is the aim, it's not enough 
to assert that "the majority" of closures are very short; the syntax 
should stand up even when used for, say, a middleware handler in a 
micro-framework. As such, I think we need additional features to opt 
back out of capturing, and explicitly mark function- or block-scoped 
variables.

On the other hand, "auto-capturing" could be seen as a feature in its 
own right; something that users will opt into when it makes sense, while 
continuing to use explicit capture in others. If that is the aim, the 
proposed syntax is decidedly sub-optimal: to a new user, there is no 
obvious reason why "fn" and "function" should imply different semantics, 
or which one is which. A dedicated syntax such as use(*) or use(...) 
would be much clearer. We could even separately propose that "fn" and 
"function" be interchangeable everywhere, allowing combinations such as 
"fn() use(...) { return $x; }" and "function() => $x;"


To go back to the point about variable scope: right now, if you're in a 
function, all variables are scoped to that function. With a tiny handful 
of exceptions (e.g. superglobals), access to variables from any other 
scope is always explicit - via parameters, "global", "use", "$this", and 
so on. If we think that should change, we should make that decision 
explicitly, not treat it as a side-effect of syntax.

I don't find the comparison to a foreach loop very convincing. Loops are 
still only accessing variables while the function is running, not saving 
them to be used at some indeterminate later time. And users don't "learn 
to recognize" that a loop doesn't hide all variables from the parent 
scope; it would be very peculiar if it did.

This is also where comparison to other languages falls down: most 
languages which capture implicitly for closures also merge scopes 
implicitly at other times - e.g. global variables in functions; instance 
properties in methods; or nested block scopes. Generally they also have 
a way to opt out of those, and mark a variable as local to a function or 
block; PHP does not, because it has always required an opt *in*.


Which leads me back to my constructive suggestion: let's introduce a 
block scoping syntax (e.g. "let $foo;") as a useful feature in its own 
right, before we introduce short closures.

As proposed, users will need to have some idea of what "live variable 
analysis" means, or add dummy assignments, if they want to be sure a 
variable is actually local. With a block scoping keyword, they can mark 
local variables explicitly, as they would in other languages.


Regards,

-- 
Rowan Tommins
[IMSoP]

---

## #117906 — Deleu — 2022-06-11T22:01:15+00:00

URL: https://externals.io/message/117888#117906 · In reply to: #117905

On Sat, Jun 11, 2022 at 11:14 PM Rowan Tommins <rowan.collins@gmail.com>
wrote:

> 
> > […]
> multi-line capabilities of long-closures with the auto-capture compactness
> of short-closures ... Arnaud Le Blanc has now picked up the flag with an
> improved implementation ... The RFC has therefore been overhauled
> accordingly and is now ready for consideration.
> > […]
>
>
> They may sound like the same thing, but to me "short closure syntax"
> (and a lot of the current RFC) implies that the new syntax is better for
> nearly all closures, and that once it is introduced, the old syntax
> would only really be there for compatibility - similar to how the []
> syntax replaces array() and list(). If that is the aim, it's not enough
> to assert that "the majority" of closures are very short; the syntax
> should stand up even when used for, say, a middleware handler in a
> micro-framework. As such, I think we need additional features to opt
> back out of capturing, and explicitly mark function- or block-scoped
> variables.
>

The RFC does mention that the existing Anonymous Function Syntax remains
untouched and will not be deprecated. Whether the new syntax is better for
nearly all closures will be a personal choice. If the new syntax doesn't
suit, say, a middleware handler, then we still can:
- reach for the old syntax
- use invocable classes
- call another method or function which creates a brand new scope and then
returns a function/callable.


>
> On the other hand, "auto-capturing" could be seen as a feature in its
> own right; something that users will opt into when it makes sense, while
> continuing to use explicit capture in others. If that is the aim, the
> proposed syntax is decidedly sub-optimal: to a new user, there is no
> obvious reason why "fn" and "function" should imply different semantics,
> or which one is which. A dedicated syntax such as use(*) or use(...)
> would be much clearer. We could even separately propose that "fn" and
> "function" be interchangeable everywhere, allowing combinations such as
> "fn() use(...) { return $x; }" and "function() => $x;"
>

The previous discussions talked about use(*) or use(...) and most people I
know that would love this RFC to pass would also dislike that alternative.
It does not have the greatest asset for short closure: aesthetics. Maybe my
personal bubble is not statistically relevant, but this is where PHP
Internals is lacking on surveying actual users of the language to help on
such matters. All I can say is that use(*) is not a replacement for the RFC.


>
> To go back to the point about variable scope: right now, if you're in a
> function, all variables are scoped to that function. With a tiny handful
> of exceptions (e.g. superglobals), access to variables from any other
> scope is always explicit - via parameters, "global", "use", "$this", and
> so on. If we think that should change, we should make that decision
> explicitly, not treat it as a side-effect of syntax.
>

Any attempt to make it explicit defeats the purpose of the RFC. The
auto-capturing means we don't have to write awkward code to access
variables. The only way we have to avoid awkward syntax (such as use
($var1, $var2)) is to declare an entire new invocable class and send the
parameters via the constructor. When many variables are involved, that may
still be a great option, but doing that just for 1 variable and 2 lines is
quite... sad. When I think of new accessors for this particular case, they
would either be innovative or verbose. If they are verbose, we already have
a syntax for that. If they are innovative, it would be an awkward
out-of-place situation that doesn't happen elsewhere in the language. Or I
lack the imagination to see a different result.



Ultimately, I see fn() as "an opt-in to not create a separate scope for a
function". PHP has several language constructs that may or may not create a
separate scope.
Delimite Scope: function, method, class, procedural file
Shared scope: if, for, foreach, include, require and fn


> Regards,
>
> --
> Rowan Tommins
> [IMSoP]
>
> --
> 
> To unsubscribe, visit: https://www.php.net/unsub.php
>
>

-- 
Marco Aurélio Deleu

---

## #117907 — Larry Garfield — 2022-06-12T01:53:30+00:00

URL: https://externals.io/message/117888#117907 · In reply to: #117906

> On Sat, Jun 11, 2022 at 11:14 PM Rowan Tommins <rowan.collins@gmail.com>
> wrote:
>
> > […]
>
> The RFC does mention that the existing Anonymous Function Syntax remains
> untouched and will not be deprecated. Whether the new syntax is better for
> nearly all closures will be a personal choice. If the new syntax doesn't
> suit, say, a middleware handler, then we still can:
> - reach for the old syntax
> - use invocable classes
> - call another method or function which creates a brand new scope and then
> returns a function/callable.

Correct.  If this RFC passes, there will be three equally supported syntaxes for creating closures:

function ($a) use ($b) {
  return $a * $b;
};

fn ($a) => $a * $b;

fn ($a) {
  return $a * $b;
};

Which one is appropriate in a given situation is left up to developer judgement.

My own personal position would be 

* use fn => where possible
* use fn {} if going mult-line.
* if the body of the closure is more than ~3 lines and is not virtually the entire wrapping scope, it should be its own named function/method *anyway*, and the new first-class-callable syntax makes that nice and easy to use.

That is, I would probably not use the manual-capture syntax very often at all.  However, if someone disagrees with me on case 3 it's still there for them if that's easier in context.

Whether the new syntax is viewed as "adding auto-capture to long closures" or "adding multi-line support to short closures" is, in the end, a mostly academic distinction with no practical difference.  The resulting syntax is smack in the middle of the two existing ones.  The original RFC from a year ago approached it from the perspective of the first;  The rewritten RFC leans on the second perspective.  The use of both names is mostly a historical artifact of reusing the old URL. :-)  The net result is the same.

--Larry Garfield

---

## #117908 — Larry Garfield — 2022-06-12T02:01:29+00:00

URL: https://externals.io/message/117888#117908 · In reply to: #117905

> 
> > […]
>
>
> First of all, thanks to all three of you for the work on this. Although 
> I'm not quite convinced yet, I know a lot of people have expressed 
> desire for this feature over the years.

> To go back to the point about variable scope: right now, if you're in a 
> function, all variables are scoped to that function. With a tiny handful 
> of exceptions (e.g. superglobals), access to variables from any other 
> scope is always explicit - via parameters, "global", "use", "$this", and 
> so on. If we think that should change, we should make that decision 
> explicitly, not treat it as a side-effect of syntax.
>
> I don't find the comparison to a foreach loop very convincing. Loops are 
> still only accessing variables while the function is running, not saving 
> them to be used at some indeterminate later time. And users don't "learn 
> to recognize" that a loop doesn't hide all variables from the parent 
> scope; it would be very peculiar if it did.

There are languages that do, however.  Some languages have block-scoped variables by default (such as Rust), or partially blocked scoped depending on details.  PHP is not one of them, but to someone coming from a language that does, PHP's way of doing things is just as weird and requires learning.  The point here is that "which things create a scope and which don't" are not "intuitive" in any language.  They're always language-idiomatic, and may or may not be internally consistent, which is the important part.

PHP is fairly internally consistent: functions and classes create a scope, nothing else does.  This RFC doesn't change that one way or another, so it's not really any harder to learn.  Plus, as noted, the `fn` keyword becomes consistently the flag saying "auto-capture happens here, FYI", which is already the case as of 7.4.

> Which leads me back to my constructive suggestion: let's introduce a 
> block scoping syntax (e.g. "let $foo;") as a useful feature in its own 
> right, before we introduce short closures.
>
> As proposed, users will need to have some idea of what "live variable 
> analysis" means, or add dummy assignments, if they want to be sure a 
> variable is actually local. With a block scoping keyword, they can mark 
> local variables explicitly, as they would in other languages.

That may be a useful feature on its own, especially for longer loops.  I'm definitely open to discussing that.  I don't think that is a prerequisite for a nicer lambda syntax, however, as I don't think the confusion potential is anywhere near as large as you apparently fear it is.

--Larry Garfield

---

## #117909 — MKS Archive — 2022-06-12T08:33:03+00:00

URL: https://externals.io/message/117888#117909 · In reply to: #117908

> 
> 
> 
> > […]
> 
> > […]
> 
> There are languages that do, however.  Some languages have block-scoped variables by default (such as Rust), or partially blocked scoped depending on details.  PHP is not one of them, but to someone coming from a language that does, PHP's way of doing things is just as weird and requires learning.  


Working in Go now for several years I'd say one of its biggest foot guns that I consistently run into when doing code reviews and even in my own code is block-level scoping where one variable shadows the same named variable outside the block and the inner variable is updated when the intention was to update the outer variable.

In short, block level scoping is a convenience that does more harm than good. At least in my experience.

Thus I would highly recommend *not* adding block level variable scope to PHP where a block in the middle of a function shadows a variable outside the block, and that variable is used below the block, such as for a return value.  

#jmtcw #fwiw

-Mike

---

## #117912 — Rowan Tommins — 2022-06-12T12:29:13+00:00

URL: https://externals.io/message/117888#117912 · In reply to: #117906

> The RFC does mention that the existing Anonymous Function Syntax 
> remains untouched and will not be deprecated. Whether the new syntax 
> is better for nearly all closures will be a personal choice.


I honestly don't think this is how it will be perceived. If this syntax 
is approved, people will see "fn" as the "new, better way" and 
"function" as the "old, annoying way".

To put it a different way: imagine we had no closure support at all, and 
decided that we needed two flavours, one with explicit capture and one 
with implicit capture. Would we choose "function" and "fn" as keywords?


> The previous discussions talked about use(*) or use(...) and most 
> people I know that would love this RFC to pass would also dislike that 
> alternative. It does not have the greatest asset for short closure: 
> aesthetics. [...] All I can say is that use(*) is not a replacement 
> for the RFC.


I think you're trying to have it both ways here: if you really believed 
that the two syntaxes were going to live side by side, there would be no 
reason for "aesthetics" to be any more important for one than the other.

Some people are of the opinion that automatic capture should always have 
been the default, and the current syntax is a mistake. I'm fine with 
that opinion, but I want people to be honest about it rather than 
pretending they're just adding a new option for a narrow use case.


> Any attempt to make it explicit defeats the purpose of the RFC.


That depends what you think the purpose of the RFC is, which is what I 
want people to be honest about.

If the purpose is to replace long lists of captured variables, an 
explicit "capture all" syntax like "use(*)" achieves that purpose 
perfectly fine.


> Ultimately, I see fn() as "an opt-in to not create a separate scope 
> for a function".


I disagree with both parts of this:

1) I don't think users will see "fn" as an "opt-in", they'll see it as 
"the new normal", and "function" as a rare "opt-out" or a "legacy version".
2) It does still create a separate scope, it just creates a *nested* 
scope, which combines two sets of variables, in a way that PHP currently 
never does.


Regards,

-- 
Rowan Tommins
[IMSoP]

---

## #117913 — Rowan Tommins — 2022-06-12T12:58:43+00:00

URL: https://externals.io/message/117888#117913 · In reply to: #117908

> > […]
> There are languages that do, however.  Some languages have block-scoped variables by default (such as Rust), or partially blocked scoped depending on details.


That's not what the RFC example implies, though; it implies that someone 
might expect $guests and $guestsIds to not be usable *inside* the 
foreach loop, because they were declared *outside* it. I don't know of 
any language where entering a loop creates a completely empty symbol 
table, do you?

Whether or not $guest, having been declared *inside* the loop, is 
visible *after* the loop is a completely different question, and one 
that doesn't apply to closures - the content of the closure hasn't been 
executed yet, so it is inevitably a black box to the code after it.


> PHP is fairly internally consistent: functions and classes create a scope, nothing else does.  This RFC doesn't change that one way or another...


The RFC fundamentally changes the rule that a function always creates a 
new, *empty* scope. Every variable that is not local to that scope has 
to be explicitly imported, one way or another.

Every language I know where scopes do *not* start out empty has keywords 
for marking which variables are definitely local to that block. That's 
why I think a "var" or "let" equivalent is a natural accompaniment to 
changing PHP's rules in this way.


> Plus, as noted, the `fn` keyword becomes consistently the flag saying "auto-capture happens here, FYI", which is already the case as of 7.4.


It's a cute idea, but I don't think "if you miss most of the letters out 
of a word, it means this special thing" is at all memorable. I've never 
heard the syntax added in 7.4 called "fn functions", but I've frequently 
heard it called "arrow functions", because what stands out to people is 
the "=>". The keyword is only there because ($a)=>$b on its own would 
collide with array syntax.

I would much rather see "fn" and "function" become synonyms, so that 
"public fn foo() {}" is a valid method declaration, and "function() => 
$foo" is a valid arrow function.


Regards,

-- 
Rowan Tommins
[IMSoP]

---

## #117914 — Deleu — 2022-06-12T15:22:15+00:00

URL: https://externals.io/message/117888#117914 · In reply to: #117912

On Sun, Jun 12, 2022 at 2:29 PM Rowan Tommins <rowan.collins@gmail.com>
wrote:

> 
> > […]
>
>
> I honestly don't think this is how it will be perceived. If this syntax
> is approved, people will see "fn" as the "new, better way" and
> "function" as the "old, annoying way".
>

And to me that's not an argument to deny what people want.


> To put it a different way: imagine we had no closure support at all, and
> decided that we needed two flavours, one with explicit capture and one
> with implicit capture. Would we choose "function" and "fn" as keywords?
>

I often don't indulge such hypotheticals because we will never truly be
able to make progress based on such an assumption. A breaking change that
changes how closure works is just not gonna happen. Given the current state
in the world we're in, what can we do to have a better DX on anonymous
functions?


>
> > […]
>
>
> I think you're trying to have it both ways here: if you really believed
> that the two syntaxes were going to live side by side, there would be no
> reason for "aesthetics" to be any more important for one than the other.
>
> Some people are of the opinion that automatic capture should always have
> been the default, and the current syntax is a mistake. I'm fine with
> that opinion, but I want people to be honest about it rather than
> pretending they're just adding a new option for a narrow use case.
>
>
Honestly I don't think it was a mistake. It was designed more than a decade
ago and there was no way of predicting the future. I've seen code written
20~10 years ago and I've seen code written 5~0 years ago. I think the best
decision was taken at the time it was taken and the world of development
has changed enough for us to make different decisions now.

It's not that I'm trying to have it both ways, I'm just not assuming my
view is the right one. I do believe that if such an RFC is approved, I will
almost never reach for `function () use ()` anymore because I will prefer
the short syntax. If I need a new scope I will reach for an invocable
class. But that doesn't mean other teams/projects/people are forced to
agree or follow the same practices as me or my team.


>
> > […]
>
>
> That depends what you think the purpose of the RFC is, which is what I
> want people to be honest about.
>
> If the purpose is to replace long lists of captured variables, an
> explicit "capture all" syntax like "use(*)" achieves that purpose
> perfectly fine.
>
>
If someone decides to implement `function () use (*)` on a separate RFC, I
would abstain from that because it's not something I'm interested in using
and it doesn't address the aesthetic issue we have today. I just don't like
it being considered an alternative to the current RFC because it's not. The
purpose is to replace long lists of captured variables while addressing the
aesthetic issue caused by `use ()`, which is the only place in the language
we use this construct.


It seems to me that you agree that there is a chance the proposed syntax is
going to be perceived as better and people will not want to use the old
syntax anymore and that makes you not want to accept the RFC.

Regards,
>
> --
> Rowan Tommins
> [IMSoP]
>
> --
> 
> To unsubscribe, visit: https://www.php.net/unsub.php
>
>

-- 
Marco Aurélio Deleu

---

## #117915 — Rowan Tommins — 2022-06-12T15:51:58+00:00

URL: https://externals.io/message/117888#117915 · In reply to: #117914

> > […]
>
>And to me that's not an argument to deny what people want.

I never said it was. I said that if that is what we expect, we should design the feature with that in mind, rather than relying on the older syntax as a crutch.



>  Given the current state in the world we're in, what can we 
> do to have a better DX on anonymous functions?

I already gave my answer to that: either add implicit capture as an opt-in to the current syntax; or add block scope and treat short closures as consistent with that.


>Honestly I don't think it was a mistake. It was designed more than a decade
>ago and there was no way of predicting the future. I've seen code written
>20~10 years ago and I've seen code written 5~0 years ago. I think the best
>decision was taken at the time it was taken and the world of development
>has changed enough for us to make different decisions now.

I've seen that argument before, but it's not clear to me that anything *has* changed. Anonymous functions are used for roughly the same things that always were, so why are the arguments made when they were added, and again when short closures were discussed previously, no longer valid?



>If someone decides to implement `function () use (*)` on a separate RFC, I
>would abstain from that because it's not something I'm interested in using

That's fair enough. Just remember that that is *your* opinion of what is important, and others may have different views.


>aesthetic issue caused by `use ()`, which is the only place in the language
>we use this construct.

That's like saying we only use the word "class" when declaring classes. It has slightly different syntax, but "use" is exactly the same principle as importing variables into scope with "global", or declaring them "static". It's entirely in keeping with how scope works in PHP.


>It seems to me that you agree that there is a chance the proposed syntax is
>going to be perceived as better and people will not want to use the old
>syntax anymore and that makes you not want to accept the RFC.

No, it makes me want to make the new syntax as useful as possible.


Regards,

-- 
Rowan Tommins
[IMSoP]

---

## #117916 — Larry Garfield — 2022-06-12T17:21:26+00:00

URL: https://externals.io/message/117888#117916 · In reply to: #117888

> Last year, Nuno Maduro and I put together an RFC for combining the 
> multi-line capabilities of long-closures with the auto-capture 
> compactness of short-closures.  That RFC didn't fully go to completion 
> due to concerns over the performance impact, which Nuno and I didn't 
> have bandwidth to resolve.
>
> Arnaud Le Blanc has now picked up the flag with an improved 
> implementation that includes benchmarks showing an effectively net-zero 
> performance impact, aka, good news as it avoids over-capturing.
>
> The RFC has therefore been overhauled accordingly and is now ready for 
> consideration.
>
> https://wiki.php.net/rfc/auto-capture-closure

A little data:

I used Nikita's project analyzer on the top 1000 projects to get a rough sense of how long-closures are used now.  All usual caveats apply about such survey data.  I was specifically looking at how many `use` statements a closure typically had, and how many statements it typically had.  Mainly, I am interested in how common "really long closures where the developer is likely to lose track of what is and isn't closed over" are.

Total closures: 20052
Total used variables: 11534

Avg capture per closure: 0.575
Avg statements per closure: 0.575

Used variable distribution (# of use variables => how many times that happens): 
        0 => 12833
        1 => 4585
        2 => 1667
        3 => 591
        4 => 198
        5 => 98
        6 => 43
        7 => 16
        8 => 9
        9 => 6
        10 => 2
        11 => 4

Statement count distribution (# of statements => how many times that happens): 
        0 => 266
        1 => 13134
        2 => 2885
        3 => 1598
        4 => 818
        5 => 429
        6 => 284
        7 => 176
        8 => 125
        9 => 88
        10 => 48
        11 => 58
        12 => 25
        13 => 27
        14 => 14
        15 => 16
        16 => 13
        17 => 7
        18 => 3
        19 => 7
        20 => 4
        21 => 5
        22 => 3
        23 => 2
        24 => 3
        26 => 2
        27 => 1
        29 => 1
        30 => 1
        35 => 1
        36 => 1
        42 => 1
        44 => 1
        48 => 1
        59 => 1
        69 => 1
        103 => 1
        122 => 1

Analysis:

* The bulk of closures close over nothing, so are irrelevant for us.
* The bulk of closures use only one statement.  That means they could easily be short-lambdas today, and are likely just pre-7.4 code that no one has bothered to update.
* The overwhelming majority of the rest are 2-3 lines long.  The dropoff after that is quite steep.  (Approximately halving each time, with a few odd exceptions.)
* Similarly, most `use` clauses contain 1-2 variables, and the dropoff after that is also quite steep.
* There's some nitwit out there writing 122 line closures, and closing over 11 variables explicitly.  Fortunately it looks like an extremely small number of nitwits. :-)

The primary target of this RFC is people writing 2-4 line closures that import 1-2 variables, both easily small enough that there should be very little risk of developers getting confused by their own code.  Based on the data above, I conclude that group is very much the typical case for closures already, and thus the risk of this syntax resulting in harder to follow code where developers get confused about what is imported and what isn't is very low.

--Larry Garfield

---

## #117917 — Mark Baker — 2022-06-12T17:54:06+00:00

URL: https://externals.io/message/117888#117917 · In reply to: #117916

> 
>
> A little data:
>
> I used Nikita's project analyzer on the top 1000 projects to get a rough sense of how long-closures are used now.  All usual caveats apply about such survey data.  I was specifically looking at how many `use` statements a closure typically had, and how many statements it typically had.  Mainly, I am interested in how common "really long closures where the developer is likely to lose track of what is and isn't closed over" are.
>
> Total closures: 20052
> Total used variables: 11534
>
Did many of those closures use "pass by reference" in the use clause, 
because that's one real differentiator between traditional closures and 
short lambdas. There's also the fact that use values are bound at the 
point where the closure is defined, not where it's called (if they even 
exist at all at that point), although that's probably more difficult to 
determine.


-- 
Mark Baker

  _________
|.  \     \-3
|_J_/ PHP |
|| |  __  |
|| |m|  |m|

  I LOVE PHP

---

## #117918 — Dan Ackroyd — 2022-06-12T18:05:02+00:00

URL: https://externals.io/message/117888#117918 · In reply to: #117888

>
> That RFC didn't fully go to completion due to concerns over the performance impact

I don't believe that is an accurate summary. There were subtle issues
in the previous RFC that should have been addressed. Nikita Popov
wrote in https://news-web.php.net/php.internals/114239

> I'm generally in favor of supporting auto-capture for multi-line closures.
>
> There are some caveats though, which this RFC should address:
>
> Subtle side-effects, visible in debugging functionality, or through destructor
> effects (the fact that a variable is captured may be observable). I think it
> nothing else, the RFC should at least make clear that this behavior
> is explicitly unspecified, and a future implementation may no longer capture
> variables where any path from entry to read passes a write.


To be clear, I don't fully understand all those issues myself (and I
have just enough knowledge to know to be scared to look at that part
of the engine) but my understanding is that the concerns are not about
just performance, they are deep concerns about the behaviour.

It would produce a better discussion if the RFC document either said
how those issues are resolved, or detail how they are still
limitations on the implementation.

It also probably would have been better (imo) to create a new RFC
document. The previous RFC went to vote, even if the vote was
cancelled. Diskspace is cheap. Having different (though similar) RFCs
under the same URL makes is confusing when trying to understand what
happened to particular RFCs.

cheers
Dan
Ack

---

## #117919 — Deleu — 2022-06-12T18:09:26+00:00

URL: https://externals.io/message/117888#117919 · In reply to: #117915

On Sun, Jun 12, 2022 at 6:55 PM Rowan Tommins <rowan.collins@gmail.com>
wrote:

>
> > […]
> is
> > […]
>
> No, it makes me want to make the new syntax as useful as possible.
>

On the sentiment, we can agree. It just happens that from where I'm
standing, any change to the proposed syntax will make it less useful.

-- 
Marco Aurélio Deleu

---

## #117920 — Larry Garfield — 2022-06-12T20:25:54+00:00

URL: https://externals.io/message/117888#117920 · In reply to: #117917

> 
> > […]
> Did many of those closures use "pass by reference" in the use clause, 
> because that's one real differentiator between traditional closures and 
> short lambdas. There's also the fact that use values are bound at the 
> point where the closure is defined, not where it's called (if they even 
> exist at all at that point), although that's probably more difficult to 
> determine.

New run to check for that:

Total used variables: 11534
ByRef used variables: 1833

So around 13% of used variables are by-ref, and thus would need to be explicitly used even with the new syntax.

> There's also the fact that use values are bound at the 
> point where the closure is defined, not where it's called (if they even 
> exist at all at that point), although that's probably more difficult to 
> determine.

I... don't see what relevance that has?  The potential for confusion is at the definition point, not call point.  If a closure is used inline then those are the same place, but if they're not, it's only the definition point that is relevant at the moment.

--Larry Garfield

---

## #117921 — Rowan Tommins — 2022-06-12T20:44:37+00:00

URL: https://externals.io/message/117888#117921 · In reply to: #117916

> The primary target of this RFC is people writing 2-4 line closures that import 1-2 variables


The first half of that sentence I was expecting - although as I've 
already said, I think the chosen syntax suggests strongly that the RFC 
is really targeting *all* closures, not any subset of them.

The second half makes much less sense. If you are only importing 1 or 2 
variables, is writing their names really that big a burden?

Several of the conversations I've had on this in the past have been very 
explicitly about the burden of large numbers of captures; if that's 
really as rare as you suggest, it makes me wonder why we're even bothering.

Regards,

-- 
Rowan Tommins
[IMSoP]

---

Next page: https://externals.io/message/117888.md?page=2
