Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:122851 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 8D4421A009C for ; Tue, 2 Apr 2024 09:24:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1712049923; bh=X7Pjf0vT/NoG4lbtf+71KY0MjKBJQPff1ByLtPbYy/g=; h=References:In-Reply-To:From:Date:Subject:To:From; b=WKGUcF+qJRpsw+7fHu7YOlvCJpJ7LIZPFFDqnJoepn5Yxb2Hp8PMTSRxbQFGZAL9i h6gFMkkQyMAiOiMbTJJRj4LDO9NL+x0/G6BJESbau4VrwAAXhaixqzDoFodwM7w1aV dYVjvIqnky7CLEVxKQOiEQwaDfJ9kbe19WhH4RVNcT1VG4VustDqgXi/n2YgSQjzCL w80cMHsbLBsX6zHme4RQNHWXeTeTbEIKVKDA8fhVL1oTBc0/uzyAq4XMUKIAo9VrRq XLhmQ4//tNy+B6Cw84DxxeX/InffA5A/n8K2Mj9rYVbCb4GPdA67P+p/25Lka/6qrV 2iGqxbEflNZxg== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 88535180069 for ; Tue, 2 Apr 2024 09:25:22 +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.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, 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-vk1-f182.google.com (mail-vk1-f182.google.com [209.85.221.182]) (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 ; Tue, 2 Apr 2024 09:25:22 +0000 (UTC) Received: by mail-vk1-f182.google.com with SMTP id 71dfb90a1353d-4d8a748881aso1620863e0c.1 for ; Tue, 02 Apr 2024 02:24:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712049893; x=1712654693; darn=lists.php.net; h=content-transfer-encoding:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=FBjW4IJdoE3d7ee+Wjqe4zobmJKR+1vKOxyQsHktQqE=; b=guMOBkpzu7YJDvQRrzonLrLRleAX32dRTeAxQzFqvUCSabsH/qrMF0drg45Omd9zEq wRuIflKI2kKH4zex9iccnW5jqNJGywK6vSPrdRAb0w0iPVR45nf/BR1eDn89VhGOmvrL DSRI8x/oYssXt5EFHTyrgn+s1gesYTkls1NlKNbCTv017wnjMh/oHTqac2yDyBvDb57x QuyHAgkg79ZUKOw2Xj1+RDER2NcsAJsUDax7RPYatBWIQCNfNwKMCWRb6LhCnsDt/PuZ i1ptVR1S7A6TSrhMFfzONXBMwkRnvzDbF2kKkzECQwObwcgipKqfWpbei1WytaxEi6Em iSFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712049893; x=1712654693; h=content-transfer-encoding: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=FBjW4IJdoE3d7ee+Wjqe4zobmJKR+1vKOxyQsHktQqE=; b=n0l4W18IFVQwFnRHtOcyphsiQO6IUMf7cttgzTDT70/IiU9WOvhmUc417sl+WIwIAo PC6qT9URahZDgjS81xIajEr5lGZDkcScaqLR1S6MOblRO2QIShFEfVPyJSAZqMRn7Sjy 0xDtot3rZg06nL4NSzG55Nt1OZwmvrTrzj0+bXVXw6DHxypUyVAnjw+PJ6K/F1umHBXo Q9LAuYlfijQH3j1SIXH3f0mf8S9hVxmyAP2maAzxBk1dF9p60UhR6xN1gR4u2OsPgjXm f+9Mr8CheZ4ZfWidXla+f2FlozY4oBna5ckBkT1QSI76PJDCz1CG/lcmCLIZ9eIFmBkF l2Zw== X-Gm-Message-State: AOJu0YyWu+RunHv15YjZzSrU1KhFr0E/YDQcnyONpMu2AfhGxAQtXKHq LdcFFhWk6X/I/QMvMtCH1sPVjAlLt+dGnHmJ3GzxR29+QAt0iiloGQuhpIMWwJ+Y5S5nczVzWDJ UV/2rlOCD9xLdq6CmRde+/RYf1KbbkwTGvQtwCfik X-Google-Smtp-Source: AGHT+IHVhjXcYvvEbRhNNFnbRIWQoZdUga2F3YGPJC+jsd5TtJ6kGR8Xj1AaIbRFsI0dUz+GXpB8oD87e13R6kmNJ3Q= X-Received: by 2002:a05:6122:3290:b0:4d8:7339:4c35 with SMTP id cj16-20020a056122329000b004d873394c35mr7665916vkb.13.1712049892979; Tue, 02 Apr 2024 02:24:52 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 References: In-Reply-To: Date: Tue, 2 Apr 2024 11:24:42 +0200 Message-ID: Subject: Re: [PHP-DEV] [RFC][Concept] Data classes (a.k.a. structs) To: PHP internals Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable From: tovilo.ilija@gmail.com (Ilija Tovilo) Hi Alexander On Tue, Apr 2, 2024 at 4:53=E2=80=AFAM Alexander Pravdin wrote: > > On Tue, Apr 2, 2024 at 9:18=E2=80=AFAM Ilija Tovilo wrote: > > > > I'd like to introduce an idea I've played around with for a couple of > > weeks: Data classes, sometimes called structs in other languages (e.g. > > Swift and C#). > > While I like the idea, I would like to suggest something else in > addition or as a separate feature. As an active user of readonly > classes with all promoted properties for data-holding purposes, I > would be happy to see the possibility of cloning them with passing > some properties to modify: > > readonly class Data { > function __construct( > public string $foo, > public string $bar, > public string $baz, > ) {} > } > > $data =3D new Data(foo: 'A', bar: 'B', baz: 'C'); > > $data2 =3D clone $data with (bar: 'X', baz: 'Y'); What you're asking for is part of the "Clone with" RFC: https://wiki.php.net/rfc/clone_with This issue is valid and the RFC would improve the ergonomics of readonly classes. However, note that it really only addresses a small part of what this RFC tries achieve: > Some APIs further exacerbate the issue by requiring multiple copies for multiple modifications (e.g. `$response->withStatus(200)->withHeader('X-foo', 'foo');`). Readonly works fine for compact data structures, even if it is copied more than it needs. For large data structures, like large lists, a copy for each modification would be detrimental. https://3v4l.org/GR6On See how the performance of an insert into an array tanks if a copy of the array is performed in each iteration (due to an additional reference to it). Readonly is just not viable for data structures such as lists, maps, sets, etc. Ilija