Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:123282 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by qa.php.net (Postfix) with ESMTPS id 7842A1A009C for ; Thu, 9 May 2024 13:11:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1715260311; bh=DVfpeSduwpCTM9uLqBUQPN3hTeHBGjv4941tMhJlJZg=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=ezm6J4Zd3UgvT+g+lu1ZfCwd845QzteDKnysIfh6OTTwrodqIapN047aMkktui3M2 hFLKwUu/Vx6MFKnirtavmDzLk0esMVUfZsCG9snn7Lkk4bhxYxZj7szIBIlBvGRDP2 h2ngTaIsJHtqCyQxeWQBQdwKxV4AXhqbi9KUBBsghm4/tmXqawsS6yLMiU3baTBPeJ vxL6vizKiqssBGJFxbrYiB/deSzb60zI+uftjeIHhU5607br4LrxLc5R3SS/sWWjeQ vozvd2YAJZRrxRM4tKdScbZoNL+k5mnIVQJhjRY8rAyoAxlKz6X5vmtTxAM/jtKhfu GwvvWG8GZ/XPA== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id DB1541805DF for ; Thu, 9 May 2024 13:11:49 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.2 required=5.0 tests=BAYES_40,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from mail-yb1-f170.google.com (mail-yb1-f170.google.com [209.85.219.170]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Thu, 9 May 2024 13:11:49 +0000 (UTC) Received: by mail-yb1-f170.google.com with SMTP id 3f1490d57ef6-de5a7b18acdso811989276.3 for ; Thu, 09 May 2024 06:11:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dqxtech.net; s=google; t=1715260259; x=1715865059; darn=lists.php.net; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=ulzauhZYmNmpZRUHe3KNy8BTX9/+ISaedwkmESvGCgY=; b=cXbEvw4SZ6zRorN2rLBxTlmWeywXwYWLHV7QqbR6wa/G5ZrxXfdWAdRD3UsIkcqaRk X0XKB/9ETAlvTs+UroqNnj1+qmBLPXnmSWkScKDJR35yISG9Xtz4LSlj3fai2p5lC1UZ simJqUiZ8YSkvMZ4gbAaLJP75iuQ4OQ220ULxFJoePnFJPl3E4IKvYmX0yn3+btIcpOp qSisafkkdtoOQkYohhbRafzk8HGHbHqwr4n/CmoAu0AmxQYrIkHc7KeAcwEXgC/vK4et RUm3D3pysZi/FYSDPg1xyd8DDlsM+f+brZDP0qrOWhJxOeTIfnIEsi2RLkLs7f520ibm qFEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715260259; x=1715865059; h=content-transfer-encoding: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=ulzauhZYmNmpZRUHe3KNy8BTX9/+ISaedwkmESvGCgY=; b=Kr5NemMUphj5bklMT1aMWWYW4UC+wFE/hpBULpBgf9CqEG7YwTzeU/ToAt27OIERu9 IcQQHdy8/Y9au527K4ZNXseybp4SGvHRbqVcOyqgSQFzVpt8Ag5QdAWDBIozh9OK5jbZ saiYzJsTMxlWs42aGXakjms2U2EGI2/1VkbuSzVrf88zKXwhHcXu4vREVNipRvtWYnFv o2oL6BLW4QD+WkIZUfz1XwRbwpiWXsJc/0UuHTWX6tDBeyfv7P+IidpDNW+GAMuYkEYv iGcOoJNkP4PuMDeAN+fu46xJh8EaSaRfCryA4ix8OZU+HiGk1ld0eqwiXmZDk8qUldvD 5hjw== X-Gm-Message-State: AOJu0YxhcXyO9852fCOmnYLJ76lDzW0W8RYwGedCfcFTkWxThrhgi0nH uppRs5fm8I2ad5YVR7tMqdgE0D44m72w9FsYq8owUxH96EcW1yAaSsQXF5zDbT7ax2jOvNAyWiw dUxNmU4F5/0hQCVSOdEn716a1mV8FqY9i5U9yGlDvd3hBoE+ODMg= X-Google-Smtp-Source: AGHT+IEIJxThYHBHknnsWN2b4Tv3rNCjTWgSjCBbVGu0iRyzVzZoyzUnCTjDhfD5LcPtLzNLGxwKna42EiB39QoL0Is= X-Received: by 2002:a25:86c7:0:b0:dcf:ad31:57c9 with SMTP id 3f1490d57ef6-debb9c1e310mr5385588276.0.1715260259639; Thu, 09 May 2024 06:10:59 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 References: <799ae864-6e25-4196-a5ce-0d74600a8378@app.fastmail.com> <280bcea8-9483-4191-80d3-81763a988290@app.fastmail.com> <5db38bc4-a6fd-4cb7-b10d-3ad1a590888c@app.fastmail.com> <39511e44-93cb-4fde-b0ec-ce24ee88f4be@app.fastmail.com> <07b0f341-f062-45fe-ade0-52cf75003895@app.fastmail.com> In-Reply-To: <07b0f341-f062-45fe-ade0-52cf75003895@app.fastmail.com> Date: Thu, 9 May 2024 15:10:48 +0200 Message-ID: Subject: Re: [PHP-DEV] [RFC] [Discussion] Clone with To: php internals Cc: Larry Garfield Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable From: andreas@dqxtech.net (Andreas Hennings) On Tue, 13 Jun 2023 at 22:13, Larry Garfield wrote= : > > On Tue, Jun 13, 2023, at 3:51 PM, M=C3=A1t=C3=A9 Kocsis wrote: > > Hi Larry, > > > > In this case, if the `with` happens first, then the new address object = is > >> cloned needlessly, but that *probably* doesn't hurt anything. But $ne= wAddr > >> !=3D=3D $p3->address. > >> > > > > Yes, I agree with this: "clone $this with ["x" =3D> "y"];" is the easie= st to > > mentally model as a shorthand for "$self =3D clone $this; $self->x =3D = "y";". > > If we agree with this model, then it would be weird to execute __clone(= ) at > > the end indeed. But I think Nicolas' example explained this fact much > > better than I could. :) Also, separating __clone() from the rest of the > > clone opcode would be . > > > > And you are right, some objects may be cloned unnecessarily, which does= n't > > hurt, but isn't ideal for sure. I should mention that in ideal > > circumstances (when all properties are readonly + all of them are > > initialized during construction + none of them are mutable internally), > > deep cloning is not 100% required, unless only for defensive programmin= g > > purposes. > > > > Regards, > > M=C3=A1t=C3=A9 > > Where all properties are readonly, and if an object those are *also* read= only, and all are assigned in the constructor... > > Yeah, that ideal case is kinda narrow, and likely will remain so for a lo= ng while yet. > > Whichever order it goes in, that should be documented explicitly and the = reasoning for it included. (Feel free to pilfer my examples above extensiv= ely if that helps.) > > As for the syntax itself, my preferences, in order, would be: > > 1. clone $foo with (...), where (...) follows named-argument syntax in al= l its variants and forms, which includes ...$arr. > > 2. clone $foo with $array, where $array is an honest to goodness assoc ar= ray/array literal, created by any means the developer wants. > > Either of those are equally expressive; the first is, IMO, cleaner and ea= sier to read/type, and probably nicer on static analysis tools, but they're= still both equally expressive. > > Anything less than that is, IMO, creating unnecessary confusion about how= the syntax behaves that will trip up developers left and right. > > --Larry Garfield Hello list, First, I am very much in favor of the named-argument syntax `clone $obj with (key: $value, ...)`, with support for argument unpacking. The syntax is well suited for the most common case, which is regular wither methods for one or more known properties. I don't like the array syntax as a default. It has inferior DX for regular withers, and possible performance impact from the temporary array. For the execution scope ideas: I kind of like the idea to have a special object state or scope effect where some properties are writable that otherwise wouldn't be. However, I don't like this kind of concept being introduced as a special case only for this clone execution scope. E.g. the current "readonly" implementations actually means write-once + write-private, applied on property level. But in the clone scope it would mean something different. I have one question though: What could be the best way to modify readonly properties from a parent clas= s? Obviously we can call a parent wither. But this means we now have two clone operations, which can have a performance impact. class C extends B { private readonly $isGreyscale; function withColor($color): static { // One clone operation in B, another in C. return =3D clone parent::withColor($color) with ( // Ok it's a bit silly to make this a separate property. isGreyscale: $color->isGreyscale(), ); } } The same problem also occurs if the parent readonly property is public. We must call the parent wither, otherwise we have no permission to update the property. One workaround is to redeclare the property in the child class. https://3v4l.org/Rn1CR I think the solutions/workarounds in both cases are acceptable, assuming that the parent wither call is rare enough. There could even be a future compiler optimization where "clone foo()" will immediately garbage-collect the temporary object, and re-use the memory space for the clone. I don't know enough about php internals to know if that would work. --- Andreas > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php >