Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:122847 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 746761A009C for ; Tue, 2 Apr 2024 02:54:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1712026468; bh=hOjs6fd/+4/snc3aEd/xzp5qmvMvU8EmAy4e49WyirI=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=C4OojU0Yf+TH3CjyvSE1QDtWUiTto9DSlpZJrTn/q8SD0rcwm+xto6j0LhQ/SoXnY PrAFhDCmUrI1NM5xSC4uONXDOojMUxbRbHw4mVdWP2GCBqaJqII4OYDLZjZwu5Xwrz ipuTU0EoGZRclYS+rw8vQnTqOWmy+zDb+C6mi2CDDiK55Na+6V1/1gu8X2R/XEpPQv qIiWbf8o71Se0bI+ViOXNc5kTVMTSSBY6J8Om8cQG/ZdL+yJtR6WoMp/CrR0b9cID8 0VOQBwG0cHeHUQj+b8ygwytvnD0eODNj6AaLOYBAYsA4xDZyzWKU4GOrOt+xBXSF/4 FYSj2mBIO32oQ== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 42F2B18006C for ; Tue, 2 Apr 2024 02:54:26 +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.8 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DMARC_MISSING,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2, SPF_HELO_NONE,SPF_NONE,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-f178.google.com (mail-yb1-f178.google.com [209.85.219.178]) (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 02:54:25 +0000 (UTC) Received: by mail-yb1-f178.google.com with SMTP id 3f1490d57ef6-dbed0710c74so3537261276.1 for ; Mon, 01 Apr 2024 19:53:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=interi-co.20230601.gappssmtp.com; s=20230601; t=1712026436; x=1712631236; 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=Cl7JJoZz2E3rGKgMy4gfFLhMwPDVnIKM84JcqZ4Tco8=; b=Nz4t3jVXOHzVznC+SofmpkTi7y85WbzCmJCrXE0DpQXa1QJ01Pa0jXPU5L1skU+fV2 eoiPej+5Tq8ECRm+KcjYrO0c/bIJrYj3hzuJt3+qYm4F/WIBTIHHf/XFRvbIM6tfYh1k Xir+OJVWt7niL4Tlh8CaJH4j5PauK2yK+eq9ufDZAxEWqTQ//N3fIlOhLbDcd1joYau6 Wq9EvZV5a2ovFEFMqEb4bSGg6uPbKvp75bwI7lH11c1MO/DIlmnURsdMBoE0IplLHdTN Sn32W4HrvspQgVsB0g+WMQDASoZR4Atch6rugX1fpBulJNT8pilUKq2XX8lE8dPTe4Sa Anfg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712026436; x=1712631236; 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=Cl7JJoZz2E3rGKgMy4gfFLhMwPDVnIKM84JcqZ4Tco8=; b=uhRqBNGR00hk5iLufidnVZdflxNb+0hIRZ9hQHf0nYaqabRzJEw+320duzyZwOxAv3 2z7aL4vkyNb/TmtwoBPdgt4odaUX0kULLCewcnDQhNWxargCOa86EnzGMpuJKNhCPT1J MgFQVmUTth8g+FXsbwBf5M5tIIid6T6kHfI3W64RFXgqpo0c5AgCXpr/Lv6ZQsuaR4qu m5+gJpVAg/AtUVByWF3RUQeE0q6VnwuXOTGrckYUWbstZ8uiSFyYpKJbDygWmKRRsKXK w/RZeCMLpjmZK99gPaX+u9CVdFwPyVavEm6aDrzHXe1Ck70LRNOj5jcKpQjG5zmBuFKX j8aQ== X-Gm-Message-State: AOJu0Yy9u4m7RIjC8aw3m0qjLAwr5TmG7b2N9vbSA4lqx45Lxd4ODTDN 022x8KodiEoBt38iwZ/XVF5vUxRqAYnSVVmNykA2dP6cKldpX759PFxzTVHfXZtA34CrsbHZMF7 xg4B67AWhUaEd+99mLfv3N7WGr4HlwqEw/rOqIg== X-Google-Smtp-Source: AGHT+IHib7n/07DeA/ZhfGlmN7crw/HnDXGuwb2KBQFmJkYDxCno6l0fx6/egu9L/9XEtS76ueYlm8VIkn4MjR3KeXs= X-Received: by 2002:a25:aa68:0:b0:dc2:41de:b744 with SMTP id s95-20020a25aa68000000b00dc241deb744mr6580633ybi.32.1712026436563; Mon, 01 Apr 2024 19:53:56 -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:53:45 +0900 Message-ID: Subject: Re: [PHP-DEV] [RFC][Concept] Data classes (a.k.a. structs) To: Ilija Tovilo Cc: PHP internals Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable From: alex.pravdin@interi.co (Alexander Pravdin) On Tue, Apr 2, 2024 at 9:18=E2=80=AFAM Ilija Tovilo wrote: > > Hi everyone! > > 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#). > > ```php > data class Vector { > private $values; > > public function __construct(...$values) { > $this->values =3D $values; > } > > public mutating function append($value) { > $this->values[] =3D $value; > } > } > > $a =3D new Vector(1, 2, 3); > $b =3D $a; > $b->append!(4); > var_dump($a); // Vector(1, 2, 3) > var_dump($b); // Vector(1, 2, 3, 4) > ``` > 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'); Under the hood, this "clone" will copy all values of promoted properties as is but modify some of them to custom values specified by the user. The implementation of this functionality in the userland destroys the beauty of readonly classes with promoted properties. Manual implementation requires a lot of code lines while bringing no sense to users who read this code. Cloning methods are bigger than the meaningful part of the class - the constructor with properties declaration. Because I have to redeclare all the properties in the method arguments and then initialize each property with a corresponding value. I love readonly classes with promoted properties for data-holding purposes and the above feature is the only one I'm missing to be completely happy. In my personal experience, I never needed to copy data classes like arrays, the immutability protects against unwanted changes enough. But copying references helps to save memory, some datasets I work with can be very big. -- Best, Alex