On Thu, Mar 25, 2021, at 12:47 AM, Peter Stalman wrote:
> On Wed, Mar 24, 2021 at 10:15 AM Chase Peeler <chasepee...@gmail.com> wrote:
> 
> > I guess my one question would be why we didn't support auto-capture when we
> > first implemented anonymous functions, and if there was a reason, why does
> > that no longer apply?
> >
> 
> I believe this was the original discussion:
> 
> https://externals.io/message/38290
> 
> Thanks,
> Peter

Thanks!

I just skimmed that thread looking for scope discussions.  I didn't read 
everything so I may have missed a comment somewhere, but the gist seems to be:

* The original proposal included manual capture because it originally was going 
to capture everything by reference, not by value, and so it was seen as "safer" 
to not surprise someone with references leaking about.  (Especially comparisons 
to Javascript.)

* There was mention in the thread of there being a performance advantage of 
manual capture, but I didn't quite follow it.  Also, that was back in 5.3 and 
so much has changed since then in the engine that I would consider any comments 
about performance from that era no longer relevant.

* There was ample debate about what syntax to use for manual capture, but very 
little discussion of whether it should be manual or automatic in the first 
place.

* In the end, we ended up with the use ($byVal, &$byRef) syntax we have 
today... but with the switch to by-val by default there was no discussion of 
whether auto-capture should be used since that was already safer.  Arguably the 
need to differentiate by val or by ref made the explicit list necessary.


So in conclusion, it seems the reasons closures weren't auto-capture always 
were:

1) Possible performance concerns that are no longer relevant.
2) Avoid surprise references.
3) Allow users to capture by value or by reference.

Point 1 is no longer relevant as the engine has changed so much.
Point 2 is no longer relevant since forever, since capture is by-val by default.
Point 3 is only relevant in the cases where you need to capture by reference, 
which in practice it turns out are really quite rare.

So, I would argue that all of the reasons for manual capture in the first place 
are no longer relevant, except in the edge case where you really do want to 
capture by reference.

Which is exactly what the RFC currently says. :-)

--Larry Garfield

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php

Reply via email to