Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:126031 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 2875B1A00BD for ; Sat, 23 Nov 2024 15:03:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1732374347; bh=91R1Ytt5CrLiwCxjg71w35l7p8hOgL8jmtUQgIwkyXs=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=aDvSY2G9cKVI0JcuOiUL1+GhmOMZ3q/vRaCiewNChg4su1SgS8i+1Ii6fdeZ6e/bU TkK9verSnCrFS/e9aSjSHINsuGBcTpLUzUQ8i1qkiApNYb59J1jK6hPWFC/O9uQGPT vQxGF9FcMtrQDYXRwVwsWypedt4BPKgqgZL5MpY3ZgxhXT6jwz4lnSr4cfCoddVgCG c+2M+JlB/LN8UfSsnaqt8l58CM15A5WbXnVidBA+cRRQEi9MOYcM0JlZU2q9Os9RgX T56fgQYyzGOfJLuTV9Iv+HwMkKkwvYHgJPmRhqxZiD9A7fHaYpyJTauaRinenOWvBg hKBT3IHMStRgw== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 2C3CA18003C for ; Sat, 23 Nov 2024 15:05:47 +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, HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE, SPF_PASS 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 ; Sat, 23 Nov 2024 15:05:43 +0000 (UTC) Received: by mail-yb1-f170.google.com with SMTP id 3f1490d57ef6-e38866c8b2dso2722286276.2 for ; Sat, 23 Nov 2024 07:03:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1732374182; x=1732978982; darn=lists.php.net; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=rNDmVlcRzo6FFV+y7bRNqa7aReosYGZ/7Wm8fnGDLhc=; b=mZ0XCVWNjebpMnlsDn21u6UR1dfnloi3p8bM7SqK4RbqZuG9pP3PTONtitrwU7FoQA a4myb92M8VwQJtdEXTnQg5ko1P9KEQqZFl97/T6n9TjL2hzqtPB+34aAh1BAp67cvRRt e3qzaadJJIgEYuXLTn/hujWlzxpiO/CI+xQ2FLDp+KniCzMeoSAeD/g2AkpYu3OdNtNU On+Wv2iOPto2lMYjRBoFf+KyiCp3f2zEdi+fnhu9mNXpkG5Lkw0sVsuLwzBg9YXsPNuQ VP0AT8GCRrMqlXJ2rcMIujt88QjGrIPqy44b2J2q9MO6Fr5SfeONhJE0iWOYAXvGdLlf 2c5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732374182; x=1732978982; 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=rNDmVlcRzo6FFV+y7bRNqa7aReosYGZ/7Wm8fnGDLhc=; b=OFl6nwDhJkVOtJEM1vDWPjnGq0gV6p/lOr34M1JzclM2aPKC/XEuo+RE1zkr5T9VWE ka/yP4gQkNOnn5LwenhRGJx63otjBjEv+uBrvFEEZT9Cw51LxhxKkP+bi1nZ9MC10evG GlCKHBu9zgm+3tzKsXfhxpWfP51K2NybvBhspMblfIKeiKerGzinNRYZ2feo2kHTvEau DH1FGJuJfcwt8DSkfudf+tUBKWh7/jNvVuXwLDAM/jYhY1Gcrm1IjC6mTzIa2XTETlFP 40baTLQ6jiUyK21QvG1PkJN5juWm10AoKoA/5yBuZxmCk1e0CWMsYkXcj4WrQZLSjoxt 08WQ== X-Gm-Message-State: AOJu0Ywzk50jfhOMAkiOkqIX7H7ZaoOYgnkBjCcZSI6aXIyxUg3t3+y3 rVRTIizB1n+Llxc89JRdx1egOfp6LNy7u0/a2fiF8moTBz+OVpAJ9IJ9+rdnMVS51wox3MCJiyh ALNRMycQnCaTIN4Jce1bK4KbkI/sRPw== X-Gm-Gg: ASbGncsCleDszJ+rvdgo9+elGLWw6pXZz+W07Me4krDZg4rJhmTy/QdK9X54HGgnjru HOZi42yLJPbF27r6iwdFpH9l9w/xO4Zb1YQ== X-Google-Smtp-Source: AGHT+IFvlx0Xy3V3wmdMic+VIF0RtemdiujcFJ9FfMsfoXK8VCogxc4f/xd6wcsbaWXdd9bx6rKmPsaZDbQCAx5xnhA= X-Received: by 2002:a05:690c:6f8b:b0:6e5:a8ce:dd0a with SMTP id 00721157ae682-6eee0b7596emr89253547b3.35.1732374181588; Sat, 23 Nov 2024 07:03:01 -0800 (PST) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 References: <18b85ba5-5f1c-489c-9096-3ae203977fbe@app.fastmail.com> In-Reply-To: <18b85ba5-5f1c-489c-9096-3ae203977fbe@app.fastmail.com> Date: Sat, 23 Nov 2024 17:02:52 +0200 Message-ID: Subject: Re: [PHP-DEV] [RFC] Data Classes To: Rob Landers Cc: internals@lists.php.net Content-Type: multipart/alternative; boundary="000000000000b63571062795cb73" From: zsidelnik@gmail.com (Eugene Sidelnyk) --000000000000b63571062795cb73 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hello If I remember correctly, the whole concept of "value" is fully described in DDD 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 keep only one instance of value object unique per given set of properties in memory, thereby eliminating cloning altogether and optimizing the memory usage. On Sat, Nov 23, 2024, 3:12 PM Rob Landers wrote: > Hello internals, > > Born from the Records RFC (https://wiki.php.net/rfc/records) discussion, > I would like to introduce to you a competing RFC: Data Classes ( > https://wiki.php.net/rfc/dataclass). > > This adds a new class modifier: data. This modifier drastically changes > how classes work, making them comparable by value instead of reference, a= nd > any mutations behave more like arrays than objects (by vale). If desired, > it can be combined with other modifiers, such as readonly, to enforce > immutability. > > I've been playing with this feature for a few days now, and it is > surprisingly intuitive to use. There is a (mostly) working implementation > available on GitHub (https://github.com/php/php-src/pull/16904) if you > want to have a go at it. > > Example: > > data class UserId { public function __construct(public int $id) {} } > > $user =3D new UserId(12); > // later > $admin =3D new UserId(12); > if ($admin =3D=3D=3D $user) { // do something } // true > > Data classes are true value objects, with full copy-on-write optimization= s: > > 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; > } > } > > I think this would be an amazing addition to PHP. > > Sincerely, > > =E2=80=94 Rob > --000000000000b63571062795cb73 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Hello

If I remember correctly, the whole concept of "value&qu= ot; is fully described in DDD book by Eric Evans. If that's the point o= f the RFC, I wonder of there's any point in not making such classes imm= utable 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.


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

Born from the Records RFC (https://wiki.php.ne= t/rfc/records) discussion, I would like to introduce to you a competing= RFC: Data Classes (https://wiki.php.net/rfc/dataclass).=C2= =A0

This adds a new class modifier: data. This= modifier drastically changes how classes work, making them comparable by v= alue instead of reference, and any mutations behave more like arrays than o= bjects (by vale). If desired, it can be combined with other modifiers, such= as readonly, to enforce immutability.

I'v= e been playing with this feature for a few days now, and it is surprisingly= intuitive to use. There is a (mostly) working implementation available on = GitHub (https://github.com/php/php-src/pull/16904) if y= ou want to have a go at it.

Example:
=

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

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

Data classes are true value objects, with full copy-on-write optimiz= ations:

data class Point {
=C2= =A0 public function __construct(public int $x, public int $y) {}
<= div>=C2=A0 public function add(Point $other): Point {
=C2=A0 = =C2=A0 // illustrating value semantics, no copy yet
=C2=A0 = =C2=A0 $previous =3D $this;
=C2=A0 =C2=A0 // a copy happens o= n the next line
=C2=A0 =C2=A0 $this->x =3D $this->x + $= other->x;
=C2=A0 =C2=A0 $this->y =3D $this->y + $oth= er->y;
=C2=A0 =C2=A0 assert($this !=3D=3D $previous); // p= asses
=C2=A0 =C2=A0 return $this;
=C2=A0 }
<= /div>
}

I think this would be an amazing a= ddition to PHP.=C2=A0

Sincerely,
=E2=80=94 Rob
--000000000000b63571062795cb73--