On 27/03/2021 16:52, Larry Garfield wrote:
It's a valid point that auto-capture on multi-line function makes the potential
for unexpected behavior higher than with a single-line function, simply by
virtue of there being more lines of code that could do so.
It's not just about number of lines, but style of code: the vast
majority of single-expression closures will have zero local variables,
so the potential for accidentally capturing a variable of the same name
is also zero.
You're right that it's subjective whether this risk is too high, though.
I was partly trying to understand what the risk is with the proposed
behaviour, and also considering whether we can reduce that risk by
changing the implementation to capture fewer variables.
This did come up in some of the previous discussions. For instance,
during the discussion of the successful RFC two years ago, Nikita wrote
[ https://externals.io/message/104693#104738 ]:
> On the other hand, allowing a block body for the closure does add a
number
> of complications to this proposal:
> [...]
> 3. Determining bound variables. For single-expression closures we can
get
> away with binding all variables that are used inside the closure.
Writing
> something like fn() $a = $b might cause an unnecessary binding of $a,
but
> it's also a very contrived situation. For block closures performing
> assignments inside the closure will be much more common and will need
some
> more consideration to avoid unnecessary bindings.
Regards,
--
Rowan Tommins
[IMSoP]
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php