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

From: Date: Tue, 14 Jun 2022 21:14:54 +0000
Subject: Re: [RFC] Short Closures 2, aka auto-capture take 3
References: 1 2 3  Groups: php.internals php.internals 
Request: Send a blank email to internals+get-117949@lists.php.net to get a copy of this message
Den 2022-06-13 kl. 14:57, skrev Arnaud Le Blanc:
On samedi 11 juin 2022 23:14:28 CEST Rowan Tommins wrote:
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*?
The proposal is to extend the Arrow Functions syntax so that it allows multiple statements. I wanted to give a name to the RFC, so that we could refer to the feature by that name instead of the longer "auto-capture multi- statement closures". But the auto-capture behavior is an important aspect we want to inherit from Arrow Functions.
As such, I think we need additional features to opt back out of capturing, and explicitly mark function- or block-scoped variables.
Currently the use() syntax co-exists with auto-capture, but we could change it so that an explicit use() list disables auto-capture instead:
fn () use ($a) { } // Would capture $a and disable auto-capture
fn () use () { }   // Would capture nothing and disable auto-capture
I like this idea very much. In the RFC two variables are captured explicitly and one implicitly.
$c = 1; fn () use ($a, &$b) { return $a + $b + $c; } I don't see the UC / value for not specifying $c while specifying $a. Think it's much clearer when capturing variables to implicitly capture everything or list the ones that should be captured. One only need to think about which variables are listed, not the ones that might be implicitly captured. Of course capturing by reference will always be required to list and if combined with capturing variables by value, they also needs to be listed. The there is this other proposal to enhance traditional anonymous functions by allowing the syntax use(*), meaning capture everything. Even if it's outside the scope of this RFC it could be mentioned in "What about Anonymous Functions?" or "Future scope". r//Björn L

« previous php.internals (#117949) next »