Newsgroups: php.internals,php.internals Path: news.php.net Xref: news.php.net php.internals:117948 php.internals:117949 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 59611 invoked from network); 14 Jun 2022 19:27:21 -0000 Received: from unknown (HELO localhost.localdomain) (76.75.200.58) by pb1.pair.com with SMTP; 14 Jun 2022 19:27:21 -0000 To: internals@lists.php.net,Arnaud Le Blanc , Message-ID: <8f70661b-67df-af12-4e07-dabd8a6154fd@telia.com> Date: Tue, 14 Jun 2022 23:14:54 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.10.0 Content-Language: en-GB Rowan Tommins Cc: php internals References: <2b35605f-8da8-46b1-aec3-00bd1bfe47fd@www.fastmail.com> <8310f3fd-0011-970e-5379-b2b6e03942b2@gmail.com> <2347345.PIDvDuAF1L@arnaud-t490> Reply-To: =?UTF-8?Q?Bj=c3=b6rn_Larsson?= In-Reply-To: <2347345.PIDvDuAF1L@arnaud-t490> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Posted-By: 84.216.97.240 Subject: Re: [PHP-DEV] [RFC] Short Closures 2, aka auto-capture take 3 From: internals@lists.php.net ("Björn Larsson via internals") 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: > > ```php > 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