Le 29/06/2021 à 15:08, Nikita Popov a écrit :
Well, it's a nifty hack :) I don't think this is the solution we want to
encourage though. It requires you pass extra information through a
side-channel -- I think I'd rather not use readonly than write that code.

Continuing along the same line, one could extend that to "clone with
argument" and do something like this:

public function __clone(self $original, array $replacements = []) {
     foreach ($original as $k => $v) {
         $this->$k = $replacements[$k] ?? $original->$k;
     }
}

and then do "clone $this(['bar' => $bar])".

In any case, I don't want to include changes to cloning in this proposal --
the topic is related, but also orthogonal to readonly properties.
Unfortunately, it will not be possible to get cloning changes into PHP 8.1
anymore, due to feature freeze.

It's okay to vote against this if cloning is a deal breaker. In that case
I'll probably either work on cloning before re-proposing this, or pivot to
asymmetric visibility -- it's not my first preference, but it may be the
more pragmatic choice. Cloning is definitely the weak point of this
proposal.

Regards,
Nikita

Hello,

I agree that the "clone with" feature, no matter how the syntax will end up being, will become vital as soon as readonly properties will be there for many people.

Nevertheless, I also agree with you, both can be done at different times, and even if it will block _some_ usages of readonly properties, readonly properties as you propose remain a must-have (in my opinion) even without the "clone with". I'd be pleased to use them an write really immutable objects, even if for cloning I need to do new Foo($otherFoo->prop1, $otherFoo->prop2), that's still a huge win in my opinion.

I can't vote, but I can't say nothing if people want to block this RFC for having the clone with at the same, whereas it could be in a RFC of its own, and readonly properties are still a huge feature on its own, even without "clone with" support. That's a very wrong reason to say no in my opinion: on the contrary, having readonly properties being accepted and implemented as-is will be a very persuasive argument in favor of the future "clone with" RFC that may emerge following this one.

--

Pierre

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

Reply via email to