2020-07-11 14:56 GMT, Rowan Tommins <rowan.coll...@gmail.com>: > Hi Olle, > > On 11 July 2020 15:06:13 BST, "Olle Härstedt" <olleharst...@gmail.com> > wrote: >>I'd like to discuss the concept of ownership in PHP, from the point of >>view of a new interface UniqueInterface (or SplUniqueInterface or >>something), which will throw a UniqueException if refcount > 1. > > It's an intriguing idea, but I'm not sure how well it would work in > practice. Maintaining a strict refcount of one means never passing the value > to another function or scope, which seems very limiting.
Yes, the point is to be limiting. :) Just like with immutability, you apply certain limitations to avoid access abuse. > > It would also run into the usual problem PHP has that everything is > determined at run-time, so every time you pass an object anywhere, the > engine would need to check it wasn't marked in this way. That could be > avoided if instead of a special type of value, this was a special type of > variable; then it could probably be determined at compile time that you were > passing a "local only" variable to another scope. > > >>Use-case: Mutable objects that cannot be shared (without (deep) >>cloning). > > This is another tricky aspect - like enforcing deep immutability, it implies > some recursive constraints which are easy to enforce for scalars, but tricky > once you look at things like arrays, which can contain references, objects, > other arrays, etc. > > > As described, the user would have to manually perform a clone whenever the > object was passed anywhere, even if they weren't mutating it anywhere. If > you let the language do that automatically, you can use an automatic > copy-on-write system, like PHP already does for arrays (and, funnily enough, > PHP 4 did for objects). > > Alternatively, you can make the object purely immutable once it has passed > out of its initial scope, or once a special function has been called to > "freeze" it. This would be an interesting solution to the "modified clone" > problem of full immutability (how to implement withFoo methods), since any > "frozen" object could be cloned to get a mutable copy, which would be frozen > before passing on. > > Regards, > > > -- > Rowan Tommins > [IMSoP] Regarding "freezing" an object, I actually already wrote about this in a blog post, using dependency injection as a use-case, and a trait to add a method to check the refcount: http://olleharstedt.github.io/php/2020/07/08/immutable-at-borrow.html -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: https://www.php.net/unsub.php