Re: [RFC] Auto-capture multi-line closures and shortfunctions take 2
| From: | Mike Schinkel | Date: | Thu, 25 Mar 2021 17:14:09 +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 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-113789@lists.php.net to get a copy of this message | ||
> On Mar 25, 2021, at 12:50 PM, Rowan Tommins <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() );
-Mike