Re: RFC Proposal: Readonly Structs in PHP
| From: | Olle Härstedt | Date: | Fri, 08 Sep 2023 14:27:07 +0000 |
| Subject: | Re: RFC Proposal: Readonly Structs in PHP | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-121014@lists.php.net to get a copy of this message | ||
2023-09-08 15:12 GMT+02:00, Lanre Waju <lanre@online-presence.ca>:
> 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