On Thu, Jul 1, 2021, at 9:49 AM, Pierre wrote:
> Le 01/07/2021 à 16:38, Nicolas Grekas a écrit :
> > Hi NIkita,
> >
> > I voted against the proposal because it doesn't work with cloning at all.
> >
> > Cloning is a critical feature of stateful objects, and we should solve it
> > the same version that introduces readonly IMHO.
> >
> > If we figure out that we can't agree on a sensible improved behavior for
> > cloning, we're going to be in a dead-end with readonly.
> >
> I respectfully disagree.
> 
> Having readonly properties and immutable objects is a must have, but 
> changing property of an object while cloning, not so much. There's many 
> case where readonly properties will be valuable where you never need to 
> clone your objects. Actually, cloning objects is not something you do 
> every day.
> 
> Please note that I agree with you that advanced / flexible clone 
> semantics would be a nice to have, but I don't see the lack of it 
> blocking for readonly properties.
> 
> I personally don't have any real use case where I couldn't implement 
> withers on my objects doing the same than dedicated advanced clone 
> semantics. Could you please provide some real world examples ? People 
> could change their minds if they could see why it's so blocking for you.
> 
> Regards,

The most famous use case right now for with-er objects is PSR-7, which is where 
the naming convention comes from.  I cannot say how widely used it is outside 
of FIG-inspired value objects, but I am pretty sure it is used.

The key point is that you rarely need to clone service objects; value objects, 
however, you have to clone if you want to mutate.  Look at any PSR-7 pipleline. 
 By design, it calls $request->withBlah($newBlah) a lot, and returns a new 
object.  That's the model that we want to support, and make *easier* to do, but 
the readonly flag makes *harder* to do than the status quo today.  (See my 
previous post on the subject in the last thread.)

There are use cases for readonly that don't require cloning.  For those, it's 
useful.  I personally think asymmetric visibility would render readonly 
unnecessary and redundant, but Nikita disagrees, and he's the one writing the 
code so... :-)

The best case scenario is by 8.2 we end up with asymmetric visibility and 
clone-with, and combined with readonly we get a huge array of options for how 
to lock down value objects and still make them evolvable.  The worst case 
scenario is we find that readonly cannot be extended to support clone-with for 
some hand-wavy engine reasons, at which point it becomes largely vestigial in 
favor of asymmetric visibility and clone-with.

--Larry Garfield

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php

Reply via email to