Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:115191 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 27326 invoked from network); 28 Jun 2021 19:45:56 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 28 Jun 2021 19:45:56 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 5490C1804B3 for ; Mon, 28 Jun 2021 13:05:46 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL, SPF_HELO_PASS,SPF_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from wout5-smtp.messagingengine.com (wout5-smtp.messagingengine.com [64.147.123.21]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Mon, 28 Jun 2021 13:05:45 -0700 (PDT) Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.west.internal (Postfix) with ESMTP id B53C33200907 for ; Mon, 28 Jun 2021 16:05:44 -0400 (EDT) Received: from imap43 ([10.202.2.93]) by compute1.internal (MEProxy); Mon, 28 Jun 2021 16:05:44 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm3; bh=HvgzHS 7hh93CnbUSUl0O838ziQHsZ9E0BquISwMRpb0=; b=GACzUCU1sROXImIllaeOim OluCM7VaUmZoLC0VIw5ZrLNSur9/rbdrszRAJHfAxU9RrS8TdEARpMCE36JbPERV Ju0us7VZ3y1OGhfendFF5W8mp7IhfjSi8+oTIXnLx/gSyKhPxTcf7mT/iMM2nTov gYuWi+LH3YowA+53RyHgjUj95uYLoqnM2maPBaG3WEcSkU7X+CRs5TR6HZ5J/LWp sXtd2HGpwSDwKKHKCXauGkgQDk1PzTJtD7jcWLoXbu88KfNil2YFNK8aoErycaMa USWlNzO+DNdO6SRnsy/pkvCaXvJjgFDqShfSh61kHSJDvoYbPP7xe0hMjMgiuAGA == X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduledrfeehgedgudeggecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpefofgggkfgjfhffhffvufgtsehttdertderredtnecuhfhrohhmpedfnfgr rhhrhicuifgrrhhfihgvlhgufdcuoehlrghrrhihsehgrghrfhhivghlughtvggthhdrtg homheqnecuggftrfgrthhtvghrnhepgeelgfekudeivddvteffueejffdthfejieevhefg ffekudevkedtvdelvddvffefnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpe hmrghilhhfrhhomheplhgrrhhrhiesghgrrhhfihgvlhguthgvtghhrdgtohhm X-ME-Proxy: Received: by mailuser.nyi.internal (Postfix, from userid 501) id 11BA6AC0076; Mon, 28 Jun 2021 16:05:44 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.5.0-alpha0-530-gd0c265785f-fm-20210616.002-gd0c26578 Mime-Version: 1.0 Message-ID: In-Reply-To: References: <24af2d01-4f6c-4389-a35a-dab50c0b5e66@www.fastmail.com> Date: Mon, 28 Jun 2021 15:05:23 -0500 To: "php internals" Content-Type: text/plain Subject: Re: [PHP-DEV] Re: [RFC] Readonly properties From: larry@garfieldtech.com ("Larry Garfield") On Mon, Jun 28, 2021, at 1:36 PM, Nicolas Grekas wrote: > >> > Actually, we talked off the list about a way to possibly make this work > >> > with __clone(): > >> > > >> > We could allow __clone to have one argument, the object being cloned. > >> And > >> > when the signature declares this argument, then all readonly properties > >> > would be set as uninitialized on $this. > >> > > >> > A typical __clone function would look like this with readonly > >> properties: > >> > function __clone(object $original) > >> > { > >> > $this->readonlyProp = clone $original->readonlyProp; > >> > } > >> > > >> > That would turn my vote into a +1 if that could be made to work! > >> > >> That sounds like it would support deep cloning, but not with-er methods. > >> There's no way to provide a changed value. It also would mean a lot of > >> work on larger objects to transfer across all the properties. I don't > >> really see what this would add. > >> > > > > Can you elaborate about the lack of support for withers? Having some work > > to do doesn't look like an issue to me, especially when there is no > > alternative to compare that too. > > > > I sent that too fast, I agree about withers... :) > I'm looking for a way to +1 that RFC... > Any other idea? IMO, the best solution to `readonly` and with-er methods is the discussed-but-not-proposed clone-with syntax. This RFC on its own: class Point { public function __construct( public readonly int $x, public readonly int $y, public readonly int $z, } public function withX(int $new): static { return new static( x: $new, y: $this->y, z: $this->z, ); } // And the same for withY() and withZ() } That does work, it's just ugly, and the amount of work needed scales exponentially with the number of properties. Same thing but adding clone-with: class Point { public function __construct( public readonly int $x, public readonly int $y, public readonly int $z, } public function withX(int $new): static { return clone($this) with {x: $new}; } // And the same for withY() and withZ() } The extra work needed here scales linearly with the number of properties. Clone-with isn't even a full proposal yet so definitely won't make it into 8.1. But, it's a standalone feature that could be added on its own later and complement `readonly`. (In case anyone hadn't noticed, I am a big fan of "features for free", which you get by designing smaller features to complement each other and become more than the sum of their parts.) The alternative is asymmetric visibility, which would allow for a withX() method like this: public function withX(int $new) { $that = clone($this); $that->x = $new; return $that; } Which is basically what withX() methods all do now already, so no change from status quo. It's more code per-property than clone-with, but still scales linearly. Nikita has made it clear he wants to proceed with readonly for now, though, and consider whether to also add asymmetric visibility in the Future(tm). I don't really see an alternative "easy fix" for readonly and with-er methods. I'll probably be voting +1 on readonly myself as is, even if it does make with-ers harder, because it would at least make the case that either clone-with or asymmetric visibility (or both, ideally) will be a necessity in 8.2. --Larry Garfield