On Thu, Mar 25, 2021 at 1:14 PM Mike Schinkel <[email protected]> wrote:
> > On Mar 25, 2021, at 12:50 PM, Rowan Tommins <[email protected]> > 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. Silly example: $counter = 0; array_map(fn($a) => {$counter++; return $a+1},$array); If you tried to pass in $counter as a parameter, it would fail. > -Mike > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php > > -- Chase Peeler [email protected]
