On Thu, 25 Mar 2021 at 10:23, Christian Schneider <cschn...@cschneid.com> wrote:
> Am 25.03.2021 um 14:29 schrieb Mark Randall <marand...@php.net>: > > On 25/03/2021 09:28, Rowan Tommins wrote: > >> That's not quite what I meant. I meant that you can't say "capture by > default, but this variable is definitely local". > > > > I think if there's one argument against, this would be it, but IMHO it > is a weakness in PHP as a whole. > > I'm not sure if I misunderstand what you're saying but to me it is one of > the greatest things about PHP that everything is local by default (minus a > narrow set of well-known and easily enough recognizable things). > > > The solution would be adding JS-like let / const statements. Which would > be a benefit to other things too. > > > I disagree that this is the solution. I think JS had to add var and later > let because of the unfortunate decision to have C-like scoping rules. > Making scoping in PHP more complex to be able to repeat this mistake in > some form seems ill-advised to me. > > - Chris > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php > > This is also my feeling. While it's easy to see from the single line fn ($x, $y) => $x + $y + $z that $z is auto-captured, it's much less easy to see that in fn ($x, $y) { $x += $y >> 2; $y = 2 - $y; $z -= 4; return $x + $y + $z; } Here's an example of this auto-catpuring multi-line functions in use today: https://github.com/hhvm/hhast/blob/bb0f0445b2e693270079dcced01a29919d87e955/src/Migrations/IsRefinementMigration.hack#L54-L111 I think that code would be more a little more readable with an explicit closure function($node, $parents) use ($map) { Lastly, PHP's handling of arrays adds an auto-capturing gotcha: fn ($x, $y) { $z[] = 1; return $x + $y + count($z); } This is valid whether or not $z is defined before the closure.