Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:120067 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 52310 invoked from network); 18 Apr 2023 15:46:10 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 18 Apr 2023 15:46:10 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 5EC6D180544 for ; Tue, 18 Apr 2023 08:46:09 -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.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW,SPF_HELO_PASS, SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS19151 66.111.4.0/24 X-Spam-Virus: No X-Envelope-From: Received: from out5-smtp.messagingengine.com (out5-smtp.messagingengine.com [66.111.4.29]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Tue, 18 Apr 2023 08:46:08 -0700 (PDT) Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id 652C25C0073 for ; Tue, 18 Apr 2023 11:46:07 -0400 (EDT) Received: from imap50 ([10.202.2.100]) by compute4.internal (MEProxy); Tue, 18 Apr 2023 11:46:07 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= garfieldtech.com; h=cc:content-transfer-encoding:content-type :content-type:date:date:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:sender:subject :subject:to:to; s=fm3; t=1681832767; x=1681919167; bh=/qhCKlQ+kJ SYBBI2buwoU793KWLcJe4OJy/FAEnuvBI=; b=y0Vdf/NN2oT1u3rPFTuHB3/iyT U4wcI27Yt0EF3b0RWbxBH9/Cib2sLvmhcppr9QhRq67ZmjkwofRg8UgHpptF22Lw 5GfKIQ26PcELPDNKSH4dFxCFXJ9vJzpOrAdhtOOOzmWyi4K8lvKeGKmUItqKK67x FNrzxLjq+eJ33V8SCZiTTnICIc/kiv6sBq2yVhkqUYb+/R/vYdFfN8lSO4cvIC/s HW9ojfYYG5yOe/q7CjTynJS5aYONGToy8w8dgI/DMLVCQ1vZketHn8fefDp6S47S 5O2q7RTJpcSK/fMyqNfqvcw8mTfadE5GjMFM0DbrNE/8YeAIgvScjYDob2Bw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm3; t=1681832767; x= 1681919167; bh=/qhCKlQ+kJSYBBI2buwoU793KWLcJe4OJy/FAEnuvBI=; b=S +Bv2stxtX1tfi33CXYeL0nPA18agI0mXuffExCGQwYWZ7BBpqwo1EM/Qoj01w7kr kSZB+k3GisRepQcqnN17SWpEYaTQFq1yq7wxh4XXVeI8Pvy9J6XgrnrCzkaeojvC ck23jifAuBIIoNsZffL71621YXYLz0Z+uspaIQfFUNLFEYjNesahbaacE0H8RAQu ygrzI8gaQ9uVxG/kF8QP2ppKR2+k6fY/b55DQ4Lxbq0FAK1HEYlEavCd+it2Lfng QyRCszpZQE4+kYiVCDpqR74dPOW8IPt1TUCdT3TKpQv6YY1hwBP/2tUrxxvTmyvI ybQqHa2Z24Y+YPYEoYHmA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrvdelkedgleefucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepofgfggfkjghffffhvffutgfgsehtqhertderreejnecuhfhrohhmpedfnfgr rhhrhicuifgrrhhfihgvlhgufdcuoehlrghrrhihsehgrghrfhhivghlughtvggthhdrtg homheqnecuggftrfgrthhtvghrnhepgeeghefgteejheeggfeghfelueeggfdtjeeivedv tefhveeguedufeelhedvteeinecuffhomhgrihhnpehphhhprdhnvghtnecuvehluhhsth gvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomheplhgrrhhrhiesghgrrhhf ihgvlhguthgvtghhrdgtohhm X-ME-Proxy: Feedback-ID: i8414410d:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id 1F57D1700089; Tue, 18 Apr 2023 11:46:07 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.9.0-alpha0-372-g43825cb665-fm-20230411.003-g43825cb6 Mime-Version: 1.0 Message-ID: <687944e3-75ec-446b-bbd6-6d3d6856e864@app.fastmail.com> In-Reply-To: References: Date: Tue, 18 Apr 2023 15:45:46 +0000 To: "php internals" Content-Type: text/plain;charset=utf-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] [RFC] [Discussion] Clone with From: larry@garfieldtech.com ("Larry Garfield") On Mon, Apr 17, 2023, at 7:43 AM, Alexandru P=C4=83tr=C4=83nescu wrote: > On Mon, Apr 17, 2023, 07:32 M=C3=A1t=C3=A9 Kocsis wrote: > >> finally I managed to create a working implementation for this feature= which >> would make it possible to properly modify readonly properties >> while simplifying how we write "wither" methods: >> https://wiki.php.net/rfc/clone_with A solid RFC, thanks! I agree with the discussion of Nicolas's alternative, which has its meri= ts but also some possible limitations. My only major concern myself is = the potential confusion between when to use : and when to use =3D>. Usi= ng the =3D> version only for dynamic keys feels very clunky. In fact, I= think the RFC is inconsistent on this front: $object =3D clone $object with {"foo" =3D> 1}; // the property name is a= literal $object =3D clone $object with {strtolower("FO") . "o" =3D> 1}; // the p= roperty name is an expression $object =3D clone $object with {PROPERTY_NAME =3D> 1}; // the property n= ame is a named constant I would expect the first one to be a colon, not fat-arrow. Is that a ty= po? Is there a technical reason why they can't just all use a colon? That w= ould extend more nicely to supporting dynamic keys in the future for nam= ed arguments. > Hey M=C3=A1t=C3=A9, > > How about just allowing a block of code after the clone statement that > would execute it in the same context as the clone context, allowing to > modify once readonly variables. > Allows better flexibility compared with clone with syntax or clone met= ho: > > public function withStatus($code, $reasonPhrase =3D ''): Response > { > return clone $this { > $this->statusCode =3D $code; > > $this->reasonPhrase =3D $reasonPhrase; > > }; This is an interesting alternative. I don't think $this is that confusi= ng, actually. Inside __clone(), $this always refers to the new object. = This proposal would essentially be providing an additional __clone bloc= k that runs in the same context, viz, $this always refers to the new obj= ect. I'm not sure if you need the original object separately at all, in= fact; if you want to look up a value from the original object... you al= ready have it on the new object in the same location, because it's a clo= ne. So: public function withStatus($code, $reasonPhrase =3D ''): Response { return clone $this { $this->statusCode =3D $code; $this->reasonPhrase =3D "Old: $this->reasonPhrase, New: $reason= Phrase"; } }; $this->reasonPhrase in the string refers to the cloned object, where tha= t value is by definition identical to the original object. It's evaluat= ed, the string is computed, and then the clone's reasonPhrase property i= s updated. The catch here is 2 fold: 1. This would essentially imply auto-capture. While I am quite OK with = that, a fair number of people are not. But adding a use() block to that= syntax just makes it very clunky for the obvious common case; which is = basically the above, so it just forces you to repeat each variable name = a third time. I would probably vote against something that forced more = redundant use() statements. 2. The scope permissioning becomes weirder. The current RFC syntax is v= ery predictable: properties get assigned from the context at which the c= lone statement lives, and if there are private/protected restrictions th= ose are self-evident. With the more closure-like approach, it "feels" l= ike you're providing an anon function that will get bound to the new obj= ect and then executed... which would then always run with private scope = and be able to modify values you probably didn't want to be publicly mod= ifiable. While we could potentially implement it such that the scope ru= les are still enforced, that may not be as obvious why you can/can't mes= s with a particular variable. I'm open to this alternative though if Mate is; it has potential, but we= 'd have to sort out the above. --Larry Garfield