Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:121023 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 5749 invoked from network); 8 Sep 2023 22:30:48 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 8 Sep 2023 22:30:48 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id D195B180083 for ; Fri, 8 Sep 2023 15:30:46 -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,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,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-pj1-f46.google.com (mail-pj1-f46.google.com [209.85.216.46]) (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 15:30:43 -0700 (PDT) Received: by mail-pj1-f46.google.com with SMTP id 98e67ed59e1d1-27183f4ccc1so1960487a91.2 for ; Fri, 08 Sep 2023 15:30:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1694212242; x=1694817042; 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=OfxfX2LLj0Jo/9ep8/ugML/sC+HBLtMQIDlpSFlAqzo=; b=sJK5mF1RGXmdNaw5BRN+TLnS0eT2lTG2UwHI8PFZar88tCFFYDWfTMpVgQlFTw8OO/ EDN1dW8g6glMuwnj2X14goNFaUHuxMoEsnIxa14YSS9xZgYTUHhpfp6m0xT/tzSWDdh3 SXaulkEmOh83oYC61A4lZt84Qitw8QzF1vXOn7MtcfWD5aQwI8CjOY87ul9Tyeb3Jhqe r4OmawJ4sCfAyrdFx7VhZzkYz9tmQc77FFZEB822MghoZSpl9sLHM/qVPcvcA5P5LDoC 6YeA2i1nw7hZD0GTerqI0h8ZMDBCkXjr8cNhcwfxHbePPp492f7nqXPdAqMF3SNtknal GENw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694212242; x=1694817042; 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=OfxfX2LLj0Jo/9ep8/ugML/sC+HBLtMQIDlpSFlAqzo=; b=rLPwbsHkj3r6TDDtVeImy0D496FJ7q39tbyMtTgSED40baCjO6Ft5d9Ns9Vw5queea xfHl+wp1O2rXVvyX24DI3EK1CrtY13xwDSThAvUK9KIL1i4z69OgjmH/xwr5DNXhXc1n GsQjgOHgmCl6tZ6RWzMIsU3PaTmKsWeh1DYjNW4lOsZQLgFwC2RnlJku9hB09uykNxkU iDHH2YXQi4nyCtIrmlHT8Z4YzZLnepEFPngv6q/nzF3rwkTeKPINIqwUL3M1Yt3DXsUz V5JQwJUrU9ZURKGczT3Czbc3QKcpBQqwwhWKwJroxWl/19+ylb7SsNL06WBxqGPBai1R ukQA== X-Gm-Message-State: AOJu0YwfqVrWuonOwpXemCTDDq4uWHHk1bL/C9tE0cg6hiOOIEbFEf5V yV49ZSmxX1NwdoNHctnn2GLIlTPFvoVVu53eBBXkpDEHxzU= X-Google-Smtp-Source: AGHT+IG01L1vcmSxbx/0kpOQ++zaK2P6ufMPM28TRFn9BSjtXiwgs/fto/XzJfrTyUwNHqiUKsJ/qf/D4aE1t+Dyfyo= X-Received: by 2002:a17:90a:1506:b0:26b:1dc1:c4bd with SMTP id l6-20020a17090a150600b0026b1dc1c4bdmr3851927pja.20.1694212242084; Fri, 08 Sep 2023 15:30:42 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Fri, 8 Sep 2023 23:30:30 +0100 Message-ID: To: Lanre Waju Cc: internals@lists.php.net Content-Type: multipart/alternative; boundary="000000000000dd1aa60604e086f4" Subject: Re: [PHP-DEV] RFC Proposal: Readonly Structs in PHP From: fenniclog@gmail.com (tag Knife) --000000000000dd1aa60604e086f4 Content-Type: text/plain; charset="UTF-8" On Fri, 8 Sept 2023 at 14:12, Lanre Waju wrote: > 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 > > I'm all data only for structures, but I really dont think they should be readonly. This type of modifier should be down to the developer to decide. If a developer only needs a data struct, they can just `struct {}` but if they need a readonly struct, they should specify the struct as `readonly struct {}` Structs can have many uses, you will eliminate 99% of them by forcing structs to be readonly. No other languages force this on the developer, PHP shouldn't either. This type of limitation would make structs DOA for most developers. --000000000000dd1aa60604e086f4--