Re: [RFC] Auto-capture multi-line closures and shortfunctions take 2

From: Date: Thu, 25 Mar 2021 18:28:55 +0000
Subject: Re: [RFC] Auto-capture multi-line closures and shortfunctions take 2
References: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15  Groups: php.internals 
Request: Send a blank email to internals+get-113795@lists.php.net to get a copy of this message
> On Mar 25, 2021, at 1:19 PM, Chase Peeler <chasepeeler@gmail.com> wrote: > > On Thu, Mar 25, 2021 at 1:14 PM Mike Schinkel <mike@newclarity.net > <mailto:mike@newclarity.net>> wrote: > > On Mar 25, 2021, at 12:50 PM, Rowan Tommins <rowan.collins@gmail.com > > <mailto:rowan.collins@gmail.com>> wrote: > > > > On 25/03/2021 15:02, Mike Schinkel wrote: > >> Can you please clarify why "function(...) use(...) {...}" can't be > >> their solution when someone needs by-reference capture? > > > > > > For the same reason - or lack of reason - why it can't be the solution when they need > > by-value capture. In other words, whatever reason people have for wanting this RFC. > > Are you proposing auto-capture but one that is note able to change the variable's value in > the outer scope? > > Since code is worth 1000 words, here is an example of what I think you are saying: > > $x = 1; > example(fn() { > echo $x; // This would print "1" > $x = 2; > echo $x; // This would print "2" > }); > echo $x; // This would still print "1" > > If that is what you are saying — which I did not get from your prior arguments — then I > myself would be fine with "by-value" capture. > > What I like about the RFC is being able to omit the use(...) when referencing (reading) a > variable inside the closure that come from the outer scope. But almost all of my use-cases would > work fine with by-value semantics, and for the rest I could use > "function(...)use(...){...}." > > That said, I again suggest this we could omit the "use" keyword for short functions: > > > // 2nd set of parens acts as an implied "use": > example( fn()(&$var) => $var = value() ); > > > That wouldn't work though if example expects a callback with a defined signature. > "Use function() use()" in that case might be a valid solution, but just wanted to throw it > out there. True. But is that really any different than this failing: example(1); When the signature is this? example(string $x) {...} -Mike

« previous php.internals (#113795) next »