Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:121011 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 67723 invoked from network); 8 Sep 2023 13:12:24 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 8 Sep 2023 13:12:24 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id CDEB61804B0 for ; Fri, 8 Sep 2023 06:12:22 -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=-0.6 required=5.0 tests=BAYES_00,BODY_8BITS, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,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 06:12:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=online-presence.ca; s=default; t=1694178741; bh=s1Ajbhi/1azrH2cEZS59DE6JLJXHQIClyJfWIf9JKQA=; h=Date:To:From:Subject:From; b=XXFQNJRuBkNH2tgcoxJlHJfDV0nCADtH6mGisIp5GGorLTs8xrHVgSI/6aYRW3L7Y YF7vOjxtxsbEgPJKhHMW5n1+tja/5FB7r5W8u+Zug8uLgJtD9ILyyDwDok5dZOzsXt mSQxEM9mzQ8zCBIQ1we6p0+SxHExccVdh4MzianvvkuepOKPurMt4+sbmCGc6lhMjj K83L8NbJkrLdTfm08njYsPcKS/D985KKy5NKpQb6ygzoIj8qjXmjpS+fK7iM/PAaGA m2ZIAarOlo7u1f/ROtTondTG/b7zJCzBLxnChjGuD71dpjpJz1E0KFAd+AZw9x0bqV /aIVN6DaU2efg== 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 5ED8A10522D for ; Fri, 8 Sep 2023 13:12:21 +0000 (UTC) Message-ID: Date: Fri, 8 Sep 2023 07:12:20 -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 To: internals@lists.php.net Content-Language: en-US Organization: Online Presence Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Subject: RFC Proposal: Readonly Structs in PHP From: lanre@online-presence.ca (Lanre Waju) Dear PHP Internals, I am writing to propose a new feature for PHP that introduces the concept of structs. This feature aims to provide a more concise and expressive way to define and work with immutable data structures. Below is a detailed description of the proposed syntax, usage, and behavior. Syntax struct Data {     string $title;     Status $status;     ?DateTimeImmutable $publishedAt = null; } The Data struct is essentially represented as a readonly class with a constructor as follows: readonly class Data {     public function __construct(         public string $title,         public Status $status,         public ?DateTimeImmutable $publishedAt = null,     ) {} } Assertions The Data struct will always be readonly. It has no methods besides the constructor. Constructors The Data struct can be constructed in three different ways, each of which allows for named or positional arguments, which can be mixed: 1.1 Class like $data = new Data('title', Status::PUBLISHED, new DateTimeImmutable()); 1.2 Class like (Named Syntax) $data = new Data(title: 'title', status: Status::PUBLISHED, publishedAt: new DateTimeImmutable()); 2.1 Proposed struct initialization syntax (Positional Arguments) $data = Data{'title', Status::PUBLISHED, new DateTimeImmutable()}; 2.2 Proposed struct initialization syntax (Named Syntax) $data = Data{title: 'title', status: Status::PUBLISHED, publishedAt: new DateTimeImmutable()}; 3.1 Anonymous Struct (Named Arguments) $data = struct {     string $title;     Status $status;     ?DateTimeImmutable $publishedAt = null; }('title', Status::PUBLISHED, new DateTimeImmutable()); 3.2 Anonymous Struct (Named Arguments - Named Syntax) $data = struct {     string $title;     Status $status;     ?DateTimeImmutable $publishedAt = null; }(title: 'title', status: Status::PUBLISHED, publishedAt: new DateTimeImmutable()); Nesting The proposed feature also supports nesting of structs. For example: final class HasNestedStruct {     NestedStruct {         string $title;         Status $status;         ?DateTimeImmutable $publishedAt = null;     };     public function __construct(         public string $string,         public Data $normalStruct,         public NestedStruct $nestedStruct = NestedStruct{'title', Status::PUBLISHED, new DateTimeImmutable()},         public struct InlineNamed { int $x} $inlineNamed = {x: 1},         public { int $x, int $y} $inlineAnonymous = {x: 1, y: 2},     ) {} } This proposal aims to enhance the readability and maintainability of code by providing a more concise and expressive way to work with immutable data structures in PHP. I believe this feature will be a valuable addition to the language as it not only opens the door for future enhancements (eg. typed json deserialization, etc.), but should also help reduce reliance on arrays by providing a more expressive alternative. Your feedback and suggestions are highly appreciated, and we look forward to discussing this proposal further within the PHP internals community. Sincerely Lanre