On Fri, Sep 8, 2023 at 2:12 PM Lanre Waju <la...@online-presence.ca> 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, > ) {} > } > These as the most basic examples of what you're proposing are barely different expressions in length, let alone anything else. I wouldn't say the first example is particularly more helpful, more readable, more concise or conceptually more expressive than the second. The rest of your examples are similarly either very minor savings of a few characters shaved off here and there, or just as verbose as instantiating a class with a constructor anyway. I'm not convinced by the rationale that this would be a new feature worthwhile for improved expression or concision. I can see a potential benefit in these "structs" (not sure that's the term//keyword I'd choose, given it has different meanings in other languages) though, as a kind of template for properly structured arrays, with a built-in ability to cast them to arrays or treat them as arrays/iterables. This would potentially help give some of the power we're missing by not having generics. So it wouldn't just be equivalent to a shorthand for a readonly class with a constructor, but one which also satisfied a couple of interfaces with the implementation automatically provided. That's my initial reaction/two cents. -Dave