Re: [RFC] Short Closures 2, aka auto-capture take 3

From: Date: Tue, 14 Jun 2022 10:29:09 +0000
Subject: Re: [RFC] Short Closures 2, aka auto-capture take 3
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to internals+get-117940@lists.php.net to get a copy of this message
Hi Dan, On lundi 13 juin 2022 19:49:10 CEST Dan Ackroyd wrote: > > Auto-capture in PHP is by-value. This makes this impossible. It also makes > > explicit declarations non-necessary and much less useful. > > Separating off some pedantism from the hopefully constructive comment, > > I think some of the words in the RFC are a bit inaccurate: > > A by-value capture means that it is not possible to modify any variables > > from the outer scope: > > > > Because variables are bound by-value, the confusing behaviors often > > associated with closures do not exist. > > > > Because variables are captured by-value, Short Closures can not have > > unintended side effects. > Those statements are true for scalar values. They are not true for objects: This is shown in the "No unintended side-effects" section of the RFC. I agree that the choice of words is inaccurate, as "modify any variable" could be interpreted not only as "bind a variable", but also as "mutate a value". The section you have quoted is meant to show how by-value capture, which is the default capture mode in all PHP closures, is less error prone than by- variable/by-reference capture, by a huge margin. Especially since variable bindings do not have side-effects unless a variable was explicitly captured by-reference. Do you agree with this ? The "No unintended side-effects" section assumes that property assignments to captured variables are intended side-effects. In your example, the programmer intended to have a side effect because $a can only come from the declaring scope (the code would result in an error otherwise) : > $a = new Foo('bar'); > $f = fn() { > $a->value = 'explicit scope is nice'; > }; Do you have an example where the intent would be less obvious ? With code you would actually write ? Cheers, -- Arnaud Le Blanc

« previous php.internals (#117940) next »