Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:122892 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 3851C1A009C for ; Tue, 2 Apr 2024 19:38:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1712086737; bh=pxaec/rTHrFC/jqwVL7NOuU/HZCfBnz1Cn7JMCsn0Go=; h=In-Reply-To:References:Date:From:To:Cc:Subject:From; b=BzApCGoTmQRTJt9GLjGZ1e+wKLnIulgdrPkXBZbAm35XA0H8b+oJEq/mXcXhzQ5AQ wdC9Weh3ZH5LJiBTNiYfmh3czDUAqaR8/A6mXptwPGsDT6XVegtWV7gmqhNqOusMMU OO7xOC6Vtb7XQyT6WYMr9vNmUCDBhv3OoBDGc2nrGMwNJPvcmKT2pqA/yj/F2wLJlB XHrTUOYUU9a2qO7P60tdm/W6z7j0Ik3YMgNUQK41PxhuvlCgVuArCsyZgnXu+pEEf0 R5gEUa7eL2n5CTZnp5mO4vhST9ndOwKnC/R9Mht/9f1zvNNNI7R6Q4emSVx6LWD/u+ YZrgaCmwjAuZQ== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 5D90F1807BC for ; Tue, 2 Apr 2024 19:38:55 +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,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from out3-smtp.messagingengine.com (out3-smtp.messagingengine.com [66.111.4.27]) (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 19:38:54 +0000 (UTC) Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.nyi.internal (Postfix) with ESMTP id 69BA25C00C4; Tue, 2 Apr 2024 15:38:25 -0400 (EDT) Received: from imap49 ([10.202.2.99]) by compute1.internal (MEProxy); Tue, 02 Apr 2024 15:38:25 -0400 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=fm3; t=1712086705; x= 1712173105; bh=GQT8JaqS1CyRW+0GKyRd1eGFsz3dkUbvon6YHdzpvzw=; b=d bxZUof6Jr1S8U51Zi+Mg6pQp1DzJb95AIZ0b7dm6Td4wyPIX6HWGohZzdkOHgOse WKWy/tmatBtMFEzAEHpjdGd/S6KMBngfKJIS1QnQWI6TWCA6VE35+c0Ld59wXolD I6/YhsiCed12tGRa+Ibg7efF+MISlzy/j4DgQ/Vnc1VBhUARFynXlzUAkRwy1kch 5APsCH54Zb5nbGfTcELS/GW8RXTxTe2YOHYXal0pjfcXhdjI307qlG9pVvMuDWsi PM3fRgmYaQf6XzmneRy485PH11Ns1WLONS8cHd96/bHY7fqfaLY/cNpdvT95Rz1l 98W+zgk6F/vwQ5WhQnhUw== 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-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; t=1712086705; x=1712173105; bh=GQT8JaqS1CyRW+0GKyRd1eGFsz3d kUbvon6YHdzpvzw=; b=MIJlEbRE4W5RQFheQeH7S+VPy2AA+J1AwjO6esP/bUc0 urTASf1K/loT6csOSY+0yLS5FsGUl/fXDl9bwMaxVLKirmJTFaLOFeOznEJMU/WD 56SkgYVJrogyVXqsh9ZhaNS03rixiOilAOl6+H1tYbOZAW/xk8bW5SN1mcRfFIEd rgiJXRkNQCFz5hTw6q56OqJbNoN8dxT+qXIqvDasyVlQT7aYQL8y6l8jMeLJo7NN rmmIXyWwNui9rVYHb85yz5Bg9fNuxZweHdFR4yfggJlFoxrJM3I5D2N3Sz+JU432 vBHG0tqhZfBHkL6iKQSHtbJJzbopcP4kGM5ReQF/GQ== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrudefvddgudeflecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpefofgggkfgjfhffhffvvefutgesrgdtreerreerjeenucfhrhhomhepfdft ohgsucfnrghnuggvrhhsfdcuoehrohgssegsohhtthhlvggurdgtohguvghsqeenucggtf frrghtthgvrhhnpedvheekteelveetfeevgeekgfffvdeuhfelveehvdetiefggedtfeej heetgffhueenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhroh hmpehrohgssegsohhtthhlvggurdgtohguvghs X-ME-Proxy: Feedback-ID: ifab94697:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id EBE3515A0092; Tue, 2 Apr 2024 15:38:24 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.11.0-alpha0-333-gbfea15422e-fm-20240327.001-gbfea1542 Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 Message-ID: In-Reply-To: References: <1a4a44c5-d4f5-40ca-b3dc-13d64c2b4425@app.fastmail.com> Date: Tue, 02 Apr 2024 21:37:50 +0200 To: "Bruce Weirdan" Cc: "php internals" Subject: Re: [PHP-DEV] [RFC][Concept] Data classes (a.k.a. structs) Content-Type: multipart/alternative; boundary=a711b7d7c03345e68cb0006cad3d5805 From: rob@bottled.codes ("Rob Landers") --a711b7d7c03345e68cb0006cad3d5805 Content-Type: text/plain;charset=utf-8 Content-Transfer-Encoding: quoted-printable On Tue, Apr 2, 2024, at 20:51, Bruce Weirdan wrote: > On Tue, Apr 2, 2024 at 8:05=E2=80=AFPM Ilija Tovilo wrote: >=20 > > Equality for data objects is based on data, rather than the object > > handle. >=20 > I believe equality should always consider the type of the object. >=20 > ```php > new Problem(size:'big') =3D=3D=3D new Universe(size:'big') > && new Problem(size:'big') =3D=3D=3D new Shoe(size:'big'); > ``` >=20 > If the above can ever be true then I'm not sure how big is the problem > (but probably very big). > Also see the examples of non-comparable ids - `new CompanyId(1)` > should not be equal to `new PersonId(1)` >=20 > And I'd find it very confusing if the following crashed >=20 > ```php > function f(Universe $_u): void {} > $universe =3D new Universe(size:'big'); > $shoe =3D new Shoe(size:'big); >=20 > if ($shoe =3D=3D=3D $universe) { > f($shoe); // shoe is *identical* to the universe, so it should be > accepted wherever the universe is > } > ``` >=20 > --=20 > Best regards, > Bruce Weirdan mailto:weirdan= @gmail.com >=20 I'd love to see it so that equality was more like =3D=3D for regular obj= ects. If the type matches and the data matches, it's true. It'd be reall= y helpful to be able to downcast types though. Such as in my user id exa= mple I gave earlier. Once it reaches a certain point in the code, it doe= sn't matter that it was once a UserId, it just matters that it is curren= tly an Id. Now that I think about it, decoration might be better than inheritance h= ere and inheritance might make more sense to be banned. In other words, = this might be just as simple and easy to use: data class Id { public function __construct(public string $id) {} } data class UserId { public function __construct(public Id $id) {} } Though it would be really interesting to use them as "traits" for each o= ther to say "this data class can be converted to another type, but infor= mation will be lost" where they are 100% separate types but can be "cast= " to specified types. // "use" has all the same rules as extends, but, // UserId is not an Id; it can be converted to an Id data class UserId use Id { public function __construct(public string $id, public string $name) {} } $user =3D new UserId('123', 'rob'); $id =3D (Id) $user; $user !=3D=3D $id =3D=3D=3D true; $id is 100% Id and lost all its "userness." Hmm. Interesting indeed. Pro= bably not practical, but interesting. =E2=80=94 Rob --a711b7d7c03345e68cb0006cad3d5805 Content-Type: text/html;charset=utf-8 Content-Transfer-Encoding: quoted-printable
On Tue, Apr 2, = 2024, at 20:51, Bruce Weirdan wrote:
On Tue, Apr 2, 2024 at 8:05=E2=80=AFPM Ilija T= ovilo <tovilo.ilija@gmail.c= om> wrote:

> Equality for data ob= jects is based on data, rather than the object
> handle= .

I believe equality should always consider= the type of the object.

```php
new Problem(size:'big') =3D=3D=3D new Universe(size:'big')
<= div>&& new Problem(size:'big') =3D=3D=3D new Shoe(size:'big');
```

If the above can ever be t= rue then I'm not sure how big is the problem
(but probably= very big).
Also see the examples of non-comparable ids - = `new CompanyId(1)`
should not be equal to `new PersonId(1)= `

And I'd find it very confusing if the fol= lowing crashed

```php
functio= n f(Universe $_u): void {}
$universe =3D new Universe(size= :'big');
$shoe =3D new Shoe(size:'big);

=
if ($shoe =3D=3D=3D $universe) {
   f= ($shoe); // shoe is *identical* to the universe, so it should be
accepted wherever the universe is
}
`= ``

-- 
  Best regar= ds,
      Bruce Weirdan &nbs= p;           &nbs= p;           &nbs= p;           mailto:weirdan@gmail.com
<= br>

I'd love to see it so that equ= ality was more like =3D=3D for regular objects. If the type matches and = the data matches, it's true. It'd be really helpful to be able to downca= st types though. Such as in my user id example I gave earlier. Once it r= eaches a certain point in the code, it doesn't matter that it was once a= UserId, it just matters that it is currently an Id.

<= /div>
Now that I think about it, decoration might be better than inh= eritance here and inheritance might make more sense to be banned. In oth= er words, this might be just as simple and easy to use:
data class Id {
  public function __cons= truct(public string $id) {}
}

data class UserId {
  public function __construct(pu= blic Id $id) {}
}

Though it w= ould be really interesting to use them as "traits" for each other to say= "this data class can be converted to another type, but information will= be lost" where they are 100% separate types but can be "cast" to specif= ied types.

// "use" has all the same rules = as extends, but,
// UserId is not an Id; it can be convert= ed to an Id
data class UserId use Id {
 = ; public function __construct(public string $id, public string $name) {}=
}

$user =3D new UserId('123', 'r= ob');

$id =3D (Id) $user;
$user !=3D=3D $id =3D=3D=3D true;

$id is 100% Id and lost all its "userness." Hmm. Interesting indeed. = Probably not practical, but interesting.

=E2=80=94 Rob
--a711b7d7c03345e68cb0006cad3d5805--