Re: [RFC] Short Closures 2, aka auto-capture take 3
| From: | Marco Pivetta | Date: | Thu, 09 Jun 2022 16:46:53 +0000 |
| Subject: | Re: [RFC] Short Closures 2, aka auto-capture take 3 | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-117889@lists.php.net to get a copy of this message | ||
Hey Larry,
On Thu, 9 Jun 2022 at 18:34, Larry Garfield <larry@garfieldtech.com> wrote:
> 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/