On Wed, 29 Jun 2022 at 18:30, Larry Garfield <la...@garfieldtech.com> wrote: > > The conversation has died down, so we'll be opening the vote for this > tomorrow.
I think I've just thought of a problem with the optimization bit of 'not capturing variables if they are written to before being used inside the closure'. Imagine some code that looks like this: // Acquire some resource e.g. an exclusive lock. $some_resource = acquire_some_resource(); $fn = fn () { // Free that resource $some_resource = null; } // do some stuff that assumes the exclusive // lock is still active. // call the callback that we 'know' frees the resource $fn(); That's a not unreasonable piece of code to write even if it's of a style many people avoid. I believe in C++ it's called "Resource acquisition is initialization", though they're trying to change the name to "Scope-Bound Resource Management" as that is a better description of what it is. With the optimization in place, that code would not behave consistently with how the rest of PHP works, where the lifetime of an object is reasonably well defined with "The destructor method will be called as soon as there are no other references to a particular object,". >From the RFC: > This approach would result in a waste of memory or CPU usage. For the record, all of my previous concerns about scoping rules have been about making code hard to reason about, and behave sanely. Memory itself is cheap. Although not having that optimization might mean that some variables last longer than they should, that is at least explainable*. Having variables not last as long as they should (because of an optimization) is harder to explain, and harder to explain how to work around. cheers Dan Ack * either use long closures or change your variable name if you don't want it captured. -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: https://www.php.net/unsub.php