Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:120115 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 75859 invoked from network); 23 Apr 2023 23:01:15 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 23 Apr 2023 23:01:15 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 9877018050B for ; Sun, 23 Apr 2023 16:01:14 -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=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM,HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-oi1-f170.google.com (mail-oi1-f170.google.com [209.85.167.170]) (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 ; Sun, 23 Apr 2023 16:01:14 -0700 (PDT) Received: by mail-oi1-f170.google.com with SMTP id 5614622812f47-38be12d59dcso2556509b6e.0 for ; Sun, 23 Apr 2023 16:01:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1682290873; x=1684882873; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=CURBqeD68CPhIPfSo3NzxkCQQXNRX9mzAeJZ3PoAYgM=; b=lgQIjbIdVZPZKVeVxPww2DCyOxrEs/BmtLI7p8+SlBcHhMl8lbIam1dXpYh33kY5te Q59Uqp9fsgosN7WDtEOg+MsmFiWhmw84Qd96tAj9d/TtVjhNw364Jy4gYeVum+hg5vXR yKe74GioFCS0mdHDQBM7AnYt0e3zbTHMeE2OQr+72nn71PJ/MfZTmW9U6aeuzaNj/pF9 Ufjld3BqD9pSCt8E8Qe3H/tBV9k2X9VvppWY8adtfKAtiwE+o7lu2KkayNfRryyc+PfP KeMOzhSaueQo8o797iPpSFttAbY8rQYmjIvUKM1cKIuqiqYX60kD4pwP7btQ7GXPsNN7 c4Ng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682290873; x=1684882873; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=CURBqeD68CPhIPfSo3NzxkCQQXNRX9mzAeJZ3PoAYgM=; b=N0zNcmKRj+1VCZaWt39stRWlCP+VoQZ3xSBkPfE089+N6bNwIAUYO59wfiMt8OYPhu ZgUauIIM/PeXuO55p5lxYPt+YTXMP90sCO8jXrXGBfjSOv1y3Pt0MJJ2U08h8AyR+YIL eAW1bGwTuwP1UB3cDXt/8X8yMEyL9V0uqr+aWuxWpD6Vz83rx0+UaJ3ZZsZvpcPDicxf Sic7bq+2dU0VNduRGtzSjQx2n1vmu0RCU6wkxzK12u4zRgKovytLaGJCWT0MkLOwN8FP hagDnuSm4gg4hGyqHG2kg/RtAptg4pvpW6J1fndyAPn0DJeZZ1+lRvKpM9BGKuC9+d5a BUyA== X-Gm-Message-State: AAQBX9foNrpGx2aAOP0HSgrmf0WdO6Zow9t0IklZJj5m7cQUDs5kXZWi mqeVJtUH41BqKKEWTYARhytffVDnTU0jlfZ+uc1l+TNu6yp3BA== X-Google-Smtp-Source: AKy350YB1R6ad8BOGdHaAviSpvq7BGA5By8CJNO98LrHRnvxmqQAY49Ds2npz4/xGi0dKQmk+znDDcIJpaig/7Lc+O8= X-Received: by 2002:a05:6808:ecc:b0:38d:ef40:139d with SMTP id q12-20020a0568080ecc00b0038def40139dmr6750732oiv.52.1682290872886; Sun, 23 Apr 2023 16:01:12 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Mon, 24 Apr 2023 01:01:01 +0200 Message-ID: To: =?UTF-8?Q?Alexandru_P=C4=83tr=C4=83nescu?= Cc: PHP Internals List Content-Type: multipart/alternative; boundary="000000000000e32c5b05fa08dde7" Subject: Re: [PHP-DEV] [RFC] [Discussion] Clone with From: kocsismate90@gmail.com (=?UTF-8?B?TcOhdMOpIEtvY3Npcw==?=) --000000000000e32c5b05fa08dde7 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hey Everyone, Thank you for the lot of feedback! Sorry, I'm going to have to answer in a single email otherwise I would have to send too many emails. Alexandru wrote: 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 method= : This is an interesting alternative, but as Tim pointed out in a later reply, the block would run in the private scope, which is contrary to my intentions (and what I consider the best solution). Micha=C5=82 wrote: I am curious if possible to implement the feature without using `with` > keyword > it visually could look pretty close to something like an object > initializer in the future: > return clone $this {c: 1}; > return new Bar {c: 1}; That's exactly what I tried first, but unfortunately, this syntax led to parser ambiguities, so at last I had to settle on using "with". Rowan wrote: 1) You mention in the Alternatives sometimes needing access to the > original instance; it would be good to have an example of how this looks > with the clone-with syntax. Makes sense, so I've just come up with an example where a linked list of objects are created. Let me know if you have a better example :) 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/4d184f960ac1b5590d87739ee3= 278c13fac157de I hope that this result is what you expect. Michal wrote: > Just noticed the "Property name expressions" and am wondering if it could > be a separate feature > allowing for passing named arguments to functions/constructors in the sam= e > fashion? As far as I can see, Nikita didn't propose the expression1() =3D> expression2() syntax in the named params RFC due to the same ambiguity I mentioned in the RFC (identifier vs. global constant). But I don't think this is set in stone, however, I do think that some optimizations would have to be disabled when param names weren't evaluatable in compile-time (https://github.com/php/php-src/pull/10831). Andreas wrote: What about argument unpacking? > I don't know if we can combine this with ":" syntax or only with "=3D>". For now, argument unpacking (property unpacking?) is not possible. But it is definitely something that could be added in the future. Tim wrote: 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 + > '=3D>' is known from match() and '=3D>' more generally is already used wi= th > 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. The= re > are plenty of places in the language where dynamic names are allowed, but > we don't just use strings for the static case 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 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-a= nd-structs/object-and-collection-initializers#object-initializers ). Regards, M=C3=A1t=C3=A9 --000000000000e32c5b05fa08dde7--