Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:121012 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 70067 invoked from network); 8 Sep 2023 13:35:27 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 8 Sep 2023 13:35:27 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 4B547180210 for ; Fri, 8 Sep 2023 06:35:26 -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-oi1-f182.google.com (mail-oi1-f182.google.com [209.85.167.182]) (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:35:22 -0700 (PDT) Received: by mail-oi1-f182.google.com with SMTP id 5614622812f47-3aa1c04668bso1315653b6e.0 for ; Fri, 08 Sep 2023 06:35:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1694180122; x=1694784922; darn=lists.php.net; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=28a8/6ArHZzHTEy0dCUz/2P84GD/uqkYFomOPyWlCBI=; b=Jt7ZmwGhfhYlcZm7ooXfwutUTEkaYGT43zfBWIqXJJgI6DlK/vvg5kEfYsMGUmF6JY MZ3xHjrIFTBW3DjSlZUKJZcDu3Sdn0geVFee2CIjqql44/cn07gx65GBNwG8LDywtx6g WvU5tUCZ8yQBZtbjIcy8lcArhcM7JbWUFXi2DCzo5zLz+ZOuKnjFK/p2e9u9RcnGEOHE OikO5TK4dftoHhilOtNK+V4Dlz/wVVkfX7+fIHyfjPLi7wPd27NUw/mYao5F6MwbHwDW Oz26fGS+tpaqRWRnR5d9KCpwNSnw7I4BBg0tH4zPdbKwObsF6WydyWGDD6egV8ZtWlle JGFg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694180122; x=1694784922; h=content-transfer-encoding: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=28a8/6ArHZzHTEy0dCUz/2P84GD/uqkYFomOPyWlCBI=; b=cXp30XY0pwcL/LZxJOY2gEljrVWKTCXkeylNC0uoqGvNnbCB5aiw1ek+MFPOeCUn6n uoReK40TUh9rqwiuNTSMeceR/tHI/xXH18UMRnNDeWUYxJy4/7B26XxabPPPNvcbnrYe WsWryX1r6Zbvfwhlv4kgX7C2WoySY1aP5gVpDzt4hjJmG6T2g0rYlG9Mwfq0e4j90UEH Sb6WyQ98CCm2kzzhQqHPeZgR6s6WXDADaORdq4XYfVO0KQRWnX6J249chnIDZ9sPX4cC bmRR/zGQqFdiJ81xgzPxhJN7wNml4fyHPCpt/xp1K0sVr68P2vL5E0dnL55Md4idEhpo CFKQ== X-Gm-Message-State: AOJu0YzS1/+hBR0uJALV3YNICt9JA0tpe/B+TFILk+Xiu0JmEXQQVrts KpInuPKd297hOOyYy9SK2DeGt9TphrDYXh4z1zBgy1im1HQ= X-Google-Smtp-Source: AGHT+IHss37q1lWSt5/xo8IzA5iCi07rYwy1qKQipVdREfaoyYqdsJBq71F7vptNZ4AxdKGKMIEHYt00B+mRyBRiQiE= X-Received: by 2002:a05:6808:2099:b0:3a4:2941:b0d4 with SMTP id s25-20020a056808209900b003a42941b0d4mr3162715oiw.24.1694180121741; Fri, 08 Sep 2023 06:35:21 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Fri, 8 Sep 2023 15:35:10 +0200 Message-ID: To: Lanre Waju Cc: internals@lists.php.net Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] RFC Proposal: Readonly Structs in PHP From: landers.robert@gmail.com (Robert Landers) On Fri, Sep 8, 2023 at 3:12=E2=80=AFPM 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 =3D 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 =3D 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 =3D new Data('title', Status::PUBLISHED, new DateTimeImmutable()); > > 1.2 Class like (Named Syntax) > $data =3D new Data(title: 'title', status: Status::PUBLISHED, publishedAt= : > new DateTimeImmutable()); > > 2.1 Proposed struct initialization syntax (Positional Arguments) > $data =3D Data{'title', Status::PUBLISHED, new DateTimeImmutable()}; > > 2.2 Proposed struct initialization syntax (Named Syntax) > $data =3D Data{title: 'title', status: Status::PUBLISHED, publishedAt: ne= w > DateTimeImmutable()}; > > 3.1 Anonymous Struct (Named Arguments) > > $data =3D struct { > string $title; > Status $status; > ?DateTimeImmutable $publishedAt =3D null; > }('title', Status::PUBLISHED, new DateTimeImmutable()); > 3.2 Anonymous Struct (Named Arguments - Named Syntax) > > $data =3D struct { > string $title; > Status $status; > ?DateTimeImmutable $publishedAt =3D 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 =3D null; > }; > > public function __construct( > public string $string, > public Data $normalStruct, > public NestedStruct $nestedStruct =3D NestedStruct{'title', > Status::PUBLISHED, new DateTimeImmutable()}, > public struct InlineNamed { int $x} $inlineNamed =3D {x: 1}, > public { int $x, int $y} $inlineAnonymous =3D {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 > FWIW (and it isn't worth much), I'm not a fan of the braces styles (2.1, 2.2) as it is very non-php-ish. It'd be great to still have methods, e.g., $data->isAfter($date) or something. Otherwise, it isn't very useful except as typed arrays, without any of the usefulness of the array functions. Also, what if I want a new $data object but with slightly different property values?