Re: RFC: Records
| From: | Nick | Date: | Mon, 21 Jul 2025 11:02:36 +0000 |
| Subject: | Re: RFC: Records | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-128159@lists.php.net to get a copy of this message | ||
> On 17. Nov 2024, at 06:14, Rob Landers <rob@bottled.codes> wrote:
>
> Hello internals,
>
> I'm ready as I'm going to be to introduce to you: "Records"
> https://wiki.php.net/rfc/records!
>
> Records allow for a lightweight syntax for defining value objects. These are superior to
> read-only classes due to having value semantics and far less boilerplate, for most things developers
> use read-only classes for. They are almost as simple to use as arrays (and provide much of the same
> semantics), but typed.
>
> As an example, if you wanted to define a simple User record:
>
> record User(string $emailAddress, int $databaseId);
>
> Then using it is as simple as calling it like a function, with the & symbol:
>
> $rob = &User("rob@bottled.codes
> <mailto:rob@bottled.codes>", 1);
>
> Since it has value semantics, we can get another instance, and it is strongly equal to another
> of the same parameters:
>
> $otherRob = &User("rob@bottled.codes
> <mailto:rob@bottled.codes>", 1);
> assert($otherRob === $rob); // true
>
> Records may also have methods (even hooks), use traits, and implement interfaces:
>
> record Vector3(float $x, float $y, $z) implements Vector {
> use Vector;
> public float magnitude {
> get => return sqrt($this->x ** 2 + $this->y ** 2 + $this->z ** 2)
> }
> }
>
> Further, an automatic (but overridable) "with" method is generated for every record.
> This allows you to get a new record similar to a given one, very easily:
>
> record Planet(string $name);
>
> $earth = &Planet("earth");
> $mars = $earth->with(name: "mars");
>
> The depth of records was an immense exploration of the PHP engine, language design, and is
> hopefully quite powerful for the needs of everyday PHP and niche libraries. I took care in every
> aspect and tried to cover every possible case in the RFC, but I still probably missed some things. I
> plan on having a full implementation done by the end of the year and open to a vote by the end of
> January, but I'd like to open the discussion up here first. Love it or hate it, I'd like
> to hear your thoughts.
>
> — Rob
Hey Rob,
We just discussed your RFCs here. We noticed this in the RFC:
```php
$point1 = &Point(3, 4);
$point2 = $point1; // No data duplication, $point2 references the same data as $point1
$point3 = Point(3, 4); // No data duplication, it is pointing to the same memory as $point1
```
Is the
& on the 3rd line Point omitted intentionally or was that just
missed?
If not missed, how is that not ambiguous?
Could you please clarify?
Cheers,
Nick