Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:121016 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 78988 invoked from network); 8 Sep 2023 15:09:43 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 8 Sep 2023 15:09:43 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 8D3631804B0 for ; Fri, 8 Sep 2023 08:09:42 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A,SPF_HELO_PASS, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS20473 216.128.176.0/20 X-Spam-Virus: No X-Envelope-From: Received: from mail.online-presence.ca (online-presence.ca [216.128.176.244]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature ECDSA (P-256) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Fri, 8 Sep 2023 08:09:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=online-presence.ca; s=default; t=1694185780; bh=bxkpdpwxnHUY8ezt0PubFv/yGq9vZBOMztRKaSMsPp8=; h=Date:Subject:To:References:From:In-Reply-To:From; b=lytVDLZcU4lYbJ0d2IdQ6NjZUkO2/U/LT56V+jdueLvjJv+bHAWXHx4cldkTIb155 kezSfIZs4rMJPXQ1d4g8UW7lbt0JGe8372YVp9q0DhB0faDmVU+54WzG00a0g6In+a keAcH8dSZRUpHpvTlpAhAZ88moNgSXQ5zLCRg0RFt17YV8W7ZqYaYgIc0C40d5GP+T c6hJAdQLQfvs4//mFt39lwounrHV6lFTa4ITwvPt2AWriiGnSgWrDn5NqfPHEkcNay I587G/KimNmU5FQ1b5z/+hQSrZpIsdplBBVvguKS1HZ4AqOhGUIncPqKhH+i0jhAN5 9GTNK+SHATMfA== Received: from [10.0.0.211] (S01064075c3d865eb.ed.shawcable.net [70.74.109.64]) (Authenticated sender: lanre@online-presence.ca) by mail.online-presence.ca (Postfix) with ESMTPSA id C305A1056EF for ; Fri, 8 Sep 2023 15:09:40 +0000 (UTC) Message-ID: <55b2131b-9de0-5b7a-83d8-92000d73f27b@online-presence.ca> Date: Fri, 8 Sep 2023 09:09:39 -0600 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.14.0 Content-Language: en-US To: internals@lists.php.net References: <9f0675b9-0474-52e7-1c0e-1186df15c5c5@online-presence.ca> Organization: Online Presence In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] RFC Proposal: Readonly Structs in PHP From: lanre@online-presence.ca (Lanre Waju) I really like the idea of making structs opaque in that sense, definitely going to put it to vote in the RFC. I agree with the decision to exclude methods from structs, as they are intended to be pure data types and If methods are required, classes can be used instead. I guess based on the response from this list i might not need to put it up to a vote. On 2023-09-08 8:58 a.m., Matthew Weier O'Phinney wrote: > On Fri, Sep 8, 2023, 9:15 AM Lanre Waju wrote: > >> Allowing Methods in Structs: >> Initially, I suggested that readonly structs have no methods besides the >> constructor. However, upon further consideration, I believe it may be >> beneficial to allow methods in structs. Even PHP enums allow methods, and >> considering this, I am open to discussing and potentially having a vote on >> allowing methods within structs as part of the RFC discussion. >> > At that point, a struct would be no different from a readonly class with > public properties, meaning we'd have two ways to accomplish the same thing. > Considering that a readonly class can already implement interfaces such as > JsonSerializable, Iterator, and ArrayAccess, they already solve the bulk of > the issues that have been raised in this thread. > > The main thing of interest that I could see from your proposal was the > ability to nest a struct in another struct or a class definition, as that > could provide some interesting type safety without the need to declare new > classes. This could be even more interesting if it allowed _any_ struct > that fulfilled the same definitions: > > class Post > { > public function __construct( > public readonly struct $author { > string $name; > UriInterface $uri; > UriInterface $avatarUri; > }, > public readonly string $title, > public readonly string $content, > public readonly DateTimeInterface $publicationDate, > ) { > } > > // ... > } > > struct User > { > string $name; > UriInterface $uri; > UriInterface $avatarUri; > string $email; > } > > $post = new Post(new User(...), ...); > > The idea here is that User fulfills the $author struct definition in the > Post class; it just has _additional_ properties. If the proposal would > allow that, this could be pretty powerful. > > I would personally stay away from solving the conversion to and from arrays > and allowing methods. If users need those, they can either use > de/serialization libraries or readonly classes, respectively. Not including > them in the initial proposal keeps it more targeted, and demonstrates a > language feature that does not currently exist. >