Re: [RFC] Auto-capture multi-line closures and shortfunctions take 2
| From: | Rowan Tommins | Date: | Sat, 27 Mar 2021 19:33:54 +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 14 15 16 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-113818@lists.php.net to get a copy of this message | ||
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]