Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:126034 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 B57721A00BD for ; Sat, 23 Nov 2024 15:43:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1732376796; bh=cWjIL5bYajJcDBn3UtpY9vWAa4/zwgVGYIJlq1AeeWA=; h=Date:From:To:Cc:In-Reply-To:References:Subject:From; b=RcC22Lqquxc9j2Htjg1h8l9tnop8fAbd58n2N90GuY7IqTs0h30RxGlQhLpwkJxFi bnViSfkohwDfPLJ+AQzeKxwW558aCKxXan/iU+PIisKDLQaizOmqXzVC47JKyiA7XV lEqexZfMfSM3JjJ30otwbOjEF+t4BLukqTO0Cs0s6SK7nqcZ53o1UfyJ/7TLudc7Pz x06nzWzkXqYq2PKYyx/sgR52g1pMhnQxbKsFUuT2H3ty4nIWJXUqUuSS0ZSkg/N5SI xHe3N7NYkn7SbefVbuHGLJvR00O0vnxqwpEKTtopHiUmWk5bhvhtpnbbNBuoDOloYe IPnyDXAZ9NELA== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 6B90E18003F for ; Sat, 23 Nov 2024 15:46:35 +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.1 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING,HTML_MESSAGE, RCVD_IN_DNSWL_LOW,SPF_HELO_PASS,SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from fhigh-b3-smtp.messagingengine.com (fhigh-b3-smtp.messagingengine.com [202.12.124.154]) (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 ; Sat, 23 Nov 2024 15:46:34 +0000 (UTC) Received: from phl-compute-01.internal (phl-compute-01.phl.internal [10.202.2.41]) by mailfhigh.stl.internal (Postfix) with ESMTP id 8F89D254012B; Sat, 23 Nov 2024 10:43:52 -0500 (EST) Received: from phl-imap-09 ([10.202.2.99]) by phl-compute-01.internal (MEProxy); Sat, 23 Nov 2024 10:43:52 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bottled.codes; h=cc:cc:content-type:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to; s=fm2; t=1732376632; x= 1732463032; bh=nxA2h7A+yJwhozu5MY2QfqF8paSE1lDzS4cIoeIaUwk=; b=I ncuEHu0UD6vaegsvjIpFXCCNUXSn6yjyFtPOKu4xIDYkKW2cwiU7JY+mSKNo5eth I5QW5xOxYG2TDXmfx0onoyOzLxeLhxfsxGdDwGr27VBwLret82FkxB/IusAoeQHR bRyRPsJcNAnoiXFPEERAvdHTUkOs/rgyxg/7kRs8Xct2AnZP7e4BEO0AaUzjhGVK aAmTuRpfobWH9/3563lnaHJ9P0ATnO3OOeYn0fzvQWTsdXjsh+v9G4/+72jW+I+W uO3qq1hWj9tpT5DwoZ7v+ZTYqUK6t4TDarH2lzaE0fAci7PZ19bUVMSdRjRdlnS/ hyNtj7ab7IZXfppDE+7pQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t= 1732376632; x=1732463032; bh=nxA2h7A+yJwhozu5MY2QfqF8paSE1lDzS4c IoeIaUwk=; b=Cb6kIyyyKZGy1S6D9WB409INGE6prvVCOU5JOrSOp1SCsk8riul 9eIeVfxLM1i+y0YOwy013A8npQO0dff/Xc3c3ssJWje7QifmNxbAzY+sMzuk4wFS nAkrEe+sXQmKZa0S7RbIHnE5YLfTgErYKH9nd/M7KPlNVR79LLQrEpD8nam69sJ/ gpGvab76MhptQfA9+xQAjgIVygWfhAsC0/NB2iusiE/KOsYKYMNv5JjJP5kh858q +FbAKlnlp/K9U+7J2ZsSN6O4EHFPRjgz6Lm3VMbPWyqs+VKUKhSXydrmppFfOPpS bn8xTA7HPDvYCiJdHANhXvgP+cVdYYPd6YQ== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefuddrgedugdejlecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdpuffr tefokffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecunecujfgurhepofggfffhvf evkfgjfhfutgesrgdtreerredtjeenucfhrhhomhepfdftohgsucfnrghnuggvrhhsfdcu oehrohgssegsohhtthhlvggurdgtohguvghsqeenucggtffrrghtthgvrhhnpeetfeffge dvudelgeekvdeuvdfhieffgffgvddujeeikedtledtgfekveefgefgfeenucffohhmrghi nhepphhhphdrnhgvthdpghhithhhuhgsrdgtohhmnecuvehluhhsthgvrhfuihiivgeptd enucfrrghrrghmpehmrghilhhfrhhomheprhhosgessghothhtlhgvugdrtghouggvshdp nhgspghrtghpthhtohepvddpmhhouggvpehsmhhtphhouhhtpdhrtghpthhtohepiihsih guvghlnhhikhesghhmrghilhdrtghomhdprhgtphhtthhopehinhhtvghrnhgrlhhssehl ihhsthhsrdhphhhprdhnvght X-ME-Proxy: Feedback-ID: ifab94697:Fastmail Received: by mailuser.phl.internal (Postfix, from userid 501) id 06148780068; Sat, 23 Nov 2024 10:43:51 -0500 (EST) X-Mailer: MessagingEngine.com Webmail Interface Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 Date: Sat, 23 Nov 2024 16:43:31 +0100 To: "Eugene Sidelnyk" Cc: internals@lists.php.net Message-ID: In-Reply-To: References: <18b85ba5-5f1c-489c-9096-3ae203977fbe@app.fastmail.com> Subject: Re: [PHP-DEV] [RFC] Data Classes Content-Type: multipart/alternative; boundary=12fcd0f9302b42cfb5b3d02857d281dd From: rob@bottled.codes ("Rob Landers") --12fcd0f9302b42cfb5b3d02857d281dd Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On Sat, Nov 23, 2024, at 16:02, Eugene Sidelnyk wrote: > Hello >=20 > If I remember correctly, the whole concept of "value" is fully describ= ed in DDD book by Eric Evans. If that's the point of the RFC, I wonder o= f there's any point in not making such classes immutable by default, and= to keep only one instance of value object unique per given set of prope= rties in memory, thereby eliminating cloning altogether and optimizing t= he memory usage. >=20 >=20 > On Sat, Nov 23, 2024, 3:12 PM Rob Landers wrote: >> __ >> Hello internals, >>=20 >> Born from the Records RFC (https://wiki.php.net/rfc/records) discussi= on, I would like to introduce to you a competing RFC: Data Classes (http= s://wiki.php.net/rfc/dataclass).=20 >>=20 >> This adds a new class modifier: data. This modifier drastically chang= es how classes work, making them comparable by value instead of referenc= e, and any mutations behave more like arrays than objects (by vale). If = desired, it can be combined with other modifiers, such as readonly, to e= nforce immutability. >>=20 >> I've been playing with this feature for a few days now, and it is sur= prisingly intuitive to use. There is a (mostly) working implementation a= vailable on GitHub (https://github.com/php/php-src/pull/16904) if you wa= nt to have a go at it. >>=20 >> Example: >>=20 >> data class UserId { public function __construct(public int $id) {} } >>=20 >> $user =3D new UserId(12); >> // later >> $admin =3D new UserId(12); >> if ($admin =3D=3D=3D $user) { // do something } // true >>=20 >> Data classes are true value objects, with full copy-on-write optimiza= tions: >>=20 >> data class Point { >> public function __construct(public int $x, public int $y) {} >> public function add(Point $other): Point { >> // illustrating value semantics, no copy yet >> $previous =3D $this; >> // a copy happens on the next line >> $this->x =3D $this->x + $other->x; >> $this->y =3D $this->y + $other->y; >> assert($this !=3D=3D $previous); // passes >> return $this; >> } >> } >>=20 >> I think this would be an amazing addition to PHP.=20 >>=20 >> Sincerely, >>=20 >> =E2=80=94 Rob Hello! Don't forget to bottom-post! > I wonder of there's any point in not making such classes immutable by = default, and to keep only one instance of value object unique per given = set of properties in memory, thereby eliminating cloning altogether and = optimizing the memory usage. This was the entire point of the records RFC ;) it was immutable by defa= ult, but people were wondering what it would look like if it were more '= composable' vs. 'dedicated syntax'. =E2=80=94 Rob --12fcd0f9302b42cfb5b3d02857d281dd Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable
On Sat, Nov 23,= 2024, at 16:02, Eugene Sidelnyk wrote:

Hello

If I = remember correctly, the whole concept of "value" is fully described in D= DD book by Eric Evans. If that's the point of the RFC, I wonder of there= 's any point in not making such classes immutable by default, and to kee= p only one instance of value object unique per given set of properties i= n memory, thereby eliminating cloning altogether and optimizing the memo= ry usage.


On Sat, Nov 23, 2024, 3:12 PM Rob Landers = <rob@bottled.codes> wrote:

Hello internals,

Born from the Records RF= C (https://wiki.php.net/rfc/records) discussion, I would l= ike to introduce to you a competing RFC: Data Classes (https= ://wiki.php.net/rfc/dataclass). 

T= his adds a new class modifier: data. This modifier drastically changes h= ow classes work, making them comparable by value instead of reference, a= nd any mutations behave more like arrays than objects (by vale). If desi= red, it can be combined with other modifiers, such as readonly, to enfor= ce immutability.

I've been playing with thi= s feature for a few days now, and it is surprisingly intuitive to use. T= here is a (mostly) working implementation available on GitHub (https://github.com/php/php-src/pull/16904) if you want to h= ave a go at it.

Example:

=
data class UserId { public function __construct(public int $i= d) {} }

$user =3D new UserId(12);
=
// later
$admin =3D new UserId(12);
if = ($admin =3D=3D=3D $user) { // do something } // true

<= /div>
Data classes are true value objects, with full copy-on-write o= ptimizations:

data class Point {
<= div>  public function __construct(public int $x, public int $y) {}<= br>
  public function add(Point $other): Point {
    // illustrating value semantics, no copy yet
    $previous =3D $this;
    //= a copy happens on the next line
    $this->x= =3D $this->x + $other->x;
    $this->y= =3D $this->y + $other->y;
    assert($thi= s !=3D=3D $previous); // passes
    return $this= ;
  }
}

I = think this would be an amazing addition to PHP. 

=
Sincerely,

=E2=80=94 Rob
=

Hello!

Don't f= orget to bottom-post!

=
I wonder of there's any point in not making such classes immutable = by default, and to keep only one instance of value object unique per giv= en set of properties in memory, thereby eliminating cloning altogether a= nd optimizing the memory usage.

This was the entire point of the records RFC ;) it was immutable by de= fault, but people were wondering what it would look like if it were more= 'composable' vs. 'dedicated syntax'.

=E2=80=94 Rob
--12fcd0f9302b42cfb5b3d02857d281dd--