Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:120119 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 34767 invoked from network); 24 Apr 2023 20:14:16 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 24 Apr 2023 20:14:16 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 61CE6180209 for ; Mon, 24 Apr 2023 13:14:15 -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.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS24940 176.9.0.0/16 X-Spam-Virus: No X-Envelope-From: Received: from chrono.xqk7.com (chrono.xqk7.com [176.9.45.72]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Mon, 24 Apr 2023 13:14:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bastelstu.be; s=mail20171119; t=1682367253; bh=KD0+lVvcKyWeAxr/ADG+rVE7YuSWT0+Z7UkLRr9OZGM=; h=Content-Type:Message-ID:Date:MIME-Version:Subject:To:Cc: References:From:In-Reply-To:from:to:cc:subject:message-id; b=ohQ4q8T7vT6yHYgcxLSGGfa4XfQnUG7dFZ7/nJC6hNZ2LIiyC2FmJCwFeJ9tBYrIm NofXz2q+SI1Crqr6dG9ytddOAU/HFVtWFEXiCSkWMuSlRwPZ2NV6JwfZ0ATj8YO8Mr babIsbT8GiH7CcAauKaQL7gKO+RZGLVdiy8lAZG8LjjRRYZe7BvoWAspNZwX2rU6Pe CNO7hOw9aT1O5OcG42d0a++Wi9QeD4627+ZAFK2mjFgTSckXmu3Jpz7rA4RfweEIuI XeqAbBG1j71Wzfbfbt8JuZSgDxeySfmJEBvMjN5UK8WO20SR2DwojHpoN+/melGNgH 5BYXtfeM0WG2Q== Content-Type: multipart/mixed; boundary="------------0M2098lVX94pvXxGMSf0oqAl" Message-ID: <21dadc4e-d0f3-1dfd-4722-ababe6714e7f@bastelstu.be> Date: Mon, 24 Apr 2023 22:14:11 +0200 MIME-Version: 1.0 Content-Language: en-US To: =?UTF-8?B?TcOhdMOpIEtvY3Npcw==?= Cc: PHP Internals List References: In-Reply-To: Subject: Re: [PHP-DEV] [RFC] [Discussion] Clone with From: tim@bastelstu.be (=?UTF-8?Q?Tim_D=c3=bcsterhus?=) --------------0M2098lVX94pvXxGMSf0oqAl Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Hi On 4/24/23 01:01, Máté Kocsis wrote: > 2) How does this interact with an __clone() method? I'm guessing the >> __clone() would be called first, and then the with-clause applied? > > > Yeah, thanks for pointing this out! I agree that the clarification is very > much needed. BTW the evaluation order > is exactly how you wrote. This is now added to the RFC. > > Tim wrote: > >> In which order will __clone(), foo(), bar(), quux(), baz() be evaluated >> and what will happen if one of them throws an Exception? Will >> destructors of the cloned object run? When? > > > In fact, after the initial ZEND_CLONE opcode (which possibly invokes > __clone()), a separate opcode is generated for each > assignment (the newly added ZEND_CLONE_INIT_PROP). This means that foo(), > bar(), quux(), and baz() will be evaluated > in this very order. If any of them throws an exception, evaluation stops > and the assignments are not rolled back. > > Regarding the destructors: yes, the destructor of the cloned object runs > immediately. In order to make sure, I've just added a test case: > https://github.com/php/php-src/pull/9497/commits/4d184f960ac1b5590d87739ee3278c13fac157de > I hope that this result is what > you expect. I'm combining your two replies, as they are related: The behavior is what I expected. It would however be useful if you also added an explicit example with side effects to "Interaction with the __clone magic method" section in the RFC and the tests (just 'echo __FUNCTION__' or so) to make it explicit. I believe the attached example should do the trick. If I understand correctly the output should be: __clone a b c d unhandled exception __destruct __destruct > I'd rather see only the fat-arrow being allowed. Unless I'm missing >> something, braces with colon is not used anywhere else, whereas braces + >> '=>' is known from match() and '=>' more generally is already used with >> array literals [1]. Having two similar syntaxes for the same thing is >> not great when both are commonly needed is not great. They need to be >> documented and learned by developers. > > > I can only repeat what Rowan answered, since I agree with it completely: > > I think it makes sense to have an unquoted form here, because the common >> case is that they are names which analysers can match statically to >> particular properties, not strings which will be analysed at runtime. There >> are plenty of places in the language where dynamic names are allowed, but >> we don't just use strings for the static case A static analyzer should be able to understand a string literal. > > However, I'm not completely sold on making "clone with" look like a > function call (return clone $this with (a: 1);), but > at least I like it more than using an array-like style (return clone $this > with [a: 1];). My intention with > using curly brackets (return clone $this with {a: 1};) is to highlight the > fact that it is a map > of key-value pairs, similarly how the JSON standard does so. Not to mention To a PHP programmer, a map of arbitrary key-value pairs is an array. That's what is familiar to every PHP programmer and thus an array literal should feel reasonably natural and obvious with regard to semantics (that's why it was my suggestion). The named parameters syntax is fairly new, but at least there is precedent and thus developers are able to transfer their existing knowledge. Both would be able to completely replace the Foo::withProperties() example by means of existing syntax (either bare array or named parameter destructuring) and without the overhead of repeated cloning. The brace + colon syntax is a completely new invention and less flexible as shown by the need to use a different separator character to differentiate between bare names and global constants. > that "clone with" serves a very > similar purpose to object initializers, and the different languages I know > to have this feature use > a similar syntax (Java: http://wiki.c2.com/?DoubleBraceInitialization, C#: > https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/object-and-collection-initializers#object-initializers > ). Different languages, different syntax choices and different existing syntax. I don't do C#, but I'm sure that the C# syntax totally makes sense in the context of C#. I do not believe it makes sense in PHP. Best regards Tim Düsterhus --------------0M2098lVX94pvXxGMSf0oqAl Content-Type: application/x-php; name="clone_with.php" Content-Disposition: attachment; filename="clone_with.php" Content-Transfer-Encoding: base64 PD9waHAKCmNsYXNzIEZvbyB7CiAgcHVibGljICRhOwogIHB1YmxpYyAkYzsKCiAgcHVibGlj IGZ1bmN0aW9uIF9fY2xvbmUoKQogIHsKICAJZWNobyBfX01FVEhPRF9fLCAiXG4iOwogIH0K ICAKICBwdWJsaWMgZnVuY3Rpb24gX19kZXN0cnVjdCgpCiAgewogICAgZWNobyBfX01FVEhP RF9fLCAiXG4iOwogIH0KfQoKZnVuY3Rpb24gYSgpIHsgZWNobyBfX0ZVTkNUSU9OX18sICJc biI7IHJldHVybiBfX0ZVTkNUSU9OX187IH0KZnVuY3Rpb24gYigpIHsgZWNobyBfX0ZVTkNU SU9OX18sICJcbiI7IHJldHVybiBfX0ZVTkNUSU9OX187IH0KZnVuY3Rpb24gYygpIHsgZWNo byBfX0ZVTkNUSU9OX18sICJcbiI7IHJldHVybiBfX0ZVTkNUSU9OX187IH0KZnVuY3Rpb24g ZCgpIHsgZWNobyBfX0ZVTkNUSU9OX18sICJcbiI7IHJldHVybiBfX0ZVTkNUSU9OX187IH0K ZnVuY3Rpb24gZSgpIHsgdGhyb3cgbmV3IFxFeGNlcHRpb24oX19GVU5DVElPTik7IH0KZnVu Y3Rpb24gZigpIHsgZWNobyBfX0ZVTkNUSU9OX18sICJcbiI7IHJldHVybiBfX0ZVTkNUSU9O X187IH0KCiRmID0gbmV3IEZvbygpOwokZyA9IGNsb25lICRmIHdpdGggewogIGEoKSA9PiBi KCksCiAgYygpID0+IGQoKSwKICBlKCkgPT4gZigpLAp9OwoKCg== --------------0M2098lVX94pvXxGMSf0oqAl--