On Thu, Jun 30, 2022 at 11:20 AM Robert Landers <landers.rob...@gmail.com> wrote: > > On Thu, Jun 30, 2022 at 10:19 AM Rowan Tommins <rowan.coll...@gmail.com> > wrote: > > > > On 29/06/2022 23:31, Dan Ackroyd wrote: > > > 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 > > > > > > For that to work, it would require the variable to be captured by > > reference, not value. Writing to a variable captured by value, like > > writing to a parameter passed by value, is just writing to a local variable. > > > > > > In fact, the "optimisation" is in my opinion a critical part of the > > semantics, to avoid the opposite problem: > > > > // Acquire some resource e.g. an exclusive lock. > > $some_resource = acquire_some_resource(); > > > > $fn = fn () { > > // Use a variable that happens to have the same name > > // A naive implementation would see $some_resource mentioned, and > > capture it > > // Over-writing the local variable here makes no difference; the > > closure still holds the value for next time > > $some_resource = 'hello'; > > } > > > > // Free what we believe is the last pointer, to trigger the destructor > > unset($some_resource); > > > > // If $some_resource gets captured, it can only be released by > > destroying the closure > > unset($fn); > > > > > > Regards, > > > > -- > > Rowan Tommins > > [IMSoP] > > > > -- > > PHP Internals - PHP Runtime Development Mailing List > > To unsubscribe, visit: https://www.php.net/unsub.php > > > > > For that to work, it would require the variable to be captured by > > reference, not value. > > I think their suggested code would work (at least currently in PHP) by > the simple fact they would increase the reference count on that > object/resource until they set it as null. However, with the > "optimization," the reference count will never be incremented and thus > fail to work as defined. > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php >
No offense, but why don't you just try it? Please see equivalents of: - Dan's code: https://3v4l.org/51jXY => doesn't "work" - Dan's code with capture by reference (as said by Rowan): https://3v4l.org/JoUVi => "works" - Rowan's code: https://3v4l.org/7ZVv3 => shows the "problem" with capture PS: I see that Rowan just replied with refcount explanations. I agree (but am sending this anyway) Regards, -- Guilliam Xavier -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: https://www.php.net/unsub.php