On 25/03/2021 16:25, Larry Garfield wrote:
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.
I wasn't involved in the discussion when anonymous functions were first
added, but I was during the discussions that led to the current arrow
functions.
I think it's important context that multi-statement closures were
originally included and explicitly removed, precisely because automatic
capture was always controversial.
It is perfectly reasonable to re-visit the topic now, but it is not, as
some seem to be implying, one that has already been agreed in principle.
Bob's original "Short Closures" RFC [1] in 2015 included both single
expressions and full function bodies. As in subsequent discussions, some
people thought implicit capture was a great feature, and others were
concerned by it. Rasmus described it [2] as "breaking [a] longstanding
basic tenet of the language".
There were other reasons that RFC was not accepted, mostly around
syntax, but several people in the voting thread [3] gave automatic
capture as a reason for voting no.
The subsequent "Arrow Functions" RFC [4] by Bob and Levi in 2017
explicitly removed multi-statement bodies from the proposal, citing
feedback from previous discussions. Bob confirmed in the discussion
thread [5] that this was a deliberate design choice, because requiring
"use" for most Closures "vastly improves readability and debuggability".
Rasmus agreed [6] that automatic capture was acceptable in the new RFC
specifically because single-expression closures "don't typically need a
local scope at all".
Finally, in 2019 Nikita wrote a new version of the RFC [7] which again
left out multi-statement bodies. During the discussion [8] he considered
adding them back in, but felt it would "add a number of complications"
which should be addressed in a future RFC.
Several people who had previously expressed strong concerns about
automatic capture voted "Yes" to Nikita's RFC . At least some presumably
did so precisely because multi-statement bodies were not included.
[1] https://wiki.php.net/rfc/short_closures
[2] https://externals.io/message/87958#87969
[3] https://externals.io/message/88394
[4] https://wiki.php.net/rfc/arrow_functions
[5] https://externals.io/message/98045#98069
[6] https://externals.io/message/98045#98108
[7] https://wiki.php.net/rfc/arrow_functions_v2
[8] https://externals.io/message/104693#104738
Regards,
--
Rowan Tommins
[IMSoP]
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php