Re: Possible RFC: UniqueInterface that throws exception at refcount > 1

From: Date: Sat, 11 Jul 2020 19:22:18 +0000
Subject: Re: Possible RFC: UniqueInterface that throws exception at refcount > 1
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-110954@lists.php.net to get a copy of this message
Hey Olle, On Sat, Jul 11, 2020 at 4:06 PM Olle Härstedt <olleharstedt@gmail.com> wrote: > Dear internals, > > 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. > > Use-case: Mutable objects that cannot be shared (without (deep) cloning). > > Rational: Immutable objects are considered "easier to reason about" > and generally more safe, and as a way to avoid > spooky-action-at-a-distance. I argue that mutability is totally fine > as long as it's not *being shared*. Currently, there's no way to > enforce this in PHP, *but* PHP has a refcounting GC which could do > this. This would be a more performant alternative than to build lots > of immutable objects using $obj->whereSomething('bla') constructs. > > Cons: If PHP switches to tracing GC, it will be unusable. > > What do you think? Should I write it down as an RFC? Implementation > should be straight-forward, assuming refcount is manipulated at a > single place in the source. > > Regards > Olle > I consider the problem problem of singleton enforcement to be solved elegantly in userland, such as in https://github.com/marc-mabe/php-enum/blob/v4.3.0/src/Enum.php Considering that, what does a language-level construct bring in? Marco Pivetta http://twitter.com/Ocramius http://ocramius.github.com/

« previous php.internals (#110954) next »