Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:121014 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 74619 invoked from network); 8 Sep 2023 14:27:09 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 8 Sep 2023 14:27:09 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id CE49D180210 for ; Fri, 8 Sep 2023 07:27:08 -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,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-il1-f173.google.com (mail-il1-f173.google.com [209.85.166.173]) (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 07:27:08 -0700 (PDT) Received: by mail-il1-f173.google.com with SMTP id e9e14a558f8ab-34ca1aadcccso7679495ab.3 for ; Fri, 08 Sep 2023 07:27:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1694183227; x=1694788027; darn=lists.php.net; h=cc:to:subject:message-id:date:from:references:in-reply-to :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=DMV1+/8U1NX5LQX22gpR7Nfnul52z1Odhb9PdTb8Ebo=; b=kXbud8D1SBTMbgZFHN/upBVmlgAI+bE/tKnDTYx7JWQ406g82kwTiW27ViSdkYjys7 YNDIN5zOlf8Jco1m32hWtyEE/qyqzbL9d5JOm2i1JX4Sh4nA5GRxIRHhL35ztvO0K7Ue BwAKmhXuWmpYuFKnzLchJB9E2Qfr9QuML20bVG/SRZI81ZGadW4CoMgLRtILosgjYedk 7DeaEZ0gBDcS1op9Gxao1B+mdXYd4cK7yZYz8/xVohAefpbDs4maFDy2S6KkCDFzBxeC 28h8g2nc6Xyn6ioeaqsHbpXS0ZByfBFU5g3z7q7b8phUfuGoNznarmUaCx38U4KDhXO7 Fssw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694183227; x=1694788027; h=cc:to:subject:message-id:date:from:references:in-reply-to :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=DMV1+/8U1NX5LQX22gpR7Nfnul52z1Odhb9PdTb8Ebo=; b=itvOg7ZT+u1fr4t21kyJ+epS6v7D7MPTcBlc89bLIcUARzqL1CMsm0GzkDRdJpcEti Jc+/mqIQ7k/f8Blbh9tAp4vDcU59o3fhDRKPEgL/FV4g9KQ1yY+G1QemeE/RnDX1f6sy z8CCl4UfYX+pDbAc3et/0vlzAl1EUL/gMu6cYidObs7u/bAIhY2WlQi+gnlufty4ulnX rbMSxfdgiuHaS5f2AjoV6ArSomC7wEoEuGmmNXBX41YI0Gd/j3UbMoZmkSGSmPnrXJLV KCge7QNmSWzvwmuGembh+63rlQFnXKd7oJ6QStXNOlblKPw5uflUzqpiMVHJU7tM43wX Mgpg== X-Gm-Message-State: AOJu0YwSCHgl1f03xokp5wmTpIzJ6riGh5PJkWwKHrk1JW1n8FAXbDl3 +TCnej4DCbMXM8omd9xheqAPbTC288jKVPNfDwLTVsNe X-Google-Smtp-Source: AGHT+IHeHytq2ulY1E+IU5+7gZH70z1z2bIrrDO1V06inTLbgwoy4fY3wQWbzBFBJhKK7X6e2bS5o5T39IffMcfT2m4= X-Received: by 2002:a05:6e02:152d:b0:34c:e4c6:c51c with SMTP id i13-20020a056e02152d00b0034ce4c6c51cmr3595073ilu.9.1694183227641; Fri, 08 Sep 2023 07:27:07 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:ac0:beca:0:b0:2ec:a632:ded5 with HTTP; Fri, 8 Sep 2023 07:27:07 -0700 (PDT) In-Reply-To: References: Date: Fri, 8 Sep 2023 16:27:07 +0200 Message-ID: To: Lanre Waju Cc: internals@lists.php.net Content-Type: text/plain; charset="UTF-8" Subject: Re: [PHP-DEV] RFC Proposal: Readonly Structs in PHP From: olleharstedt@gmail.com (=?UTF-8?Q?Olle_H=C3=A4rstedt?=) 2023-09-08 15:12 GMT+02:00, 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 > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php Aren't structs value objects in C#? Might be some semantic confusion there, in choice of words. https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/struct "Structure types have value semantics". Java uses the "record" keyword. Worth taking a look at, maybe? Olle