Re: [RFC] Short Closures 2, aka auto-capture take 3
| From: | Larry Garfield | Date: | Wed, 15 Jun 2022 23:59:33 +0000 |
| Subject: | Re: [RFC] Short Closures 2, aka auto-capture take 3 | ||
| References: | 1 2 3 4 5 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-117957@lists.php.net to get a copy of this message | ||
On Wed, Jun 15, 2022, at 7:05 AM, Guilliam Xavier wrote:
>> > > Because variables are captured by-value, Short Closures can not have
>> > > unintended side effects.
>> >
>> > Those statements are true for scalar values. They are not true for objects:
>>
>> This is shown in the "No unintended side-effects" section of the RFC.
>
> I'm confused by the last example:
>
> $fn2 = function () use (&$a) { /* code with $a AND $b */ }
>
> Isn't that missing a ", $b" in the
use?
>
> And like others, I also find that allowing mixing explicit *by-value*
> capture with auto-capture is not really needed and even confusing; if
> you "expect that explicitly capturing by value will be rare in
> practice" you might as well forbid it?
Arnaud and I discussed it, and we're going to drop the mix-autocapture-and-manual
functionality. I was tepid on it to begin with, and it can be confusing. RFC will be updated soon.
> By the way, what about *arrow* functions? e.g.
>
> $fn = fn () use (&$ref) => $ref = $val1 + $val2; // assigns and returns
>
> Would that be allowed? Is it really *desirable*?
I don't think it's really desireable. By-ref closure is unusual, probably even less so in
one line closures (though I've not checked that specifically), references are usually a bad
idea anyway, and in those unusual cases the long-form is still there if you want to control
everything.
--Larry Garfield