On Fri, 15 Mar 2019 at 09:54, Nikita Popov <nikita....@gmail.com> wrote:
> 2. Readonly properties: Without requiring an entire object to be immutable, > we can add support for readonly properties. This may be either a standalone > feature, or part of / based on a more general property accessors proposal. > This is reasonable, but only if true immutability is the aim, rather than pass-by-value / copy-on-write. True immutability is probably fine for small structs, like Point(x, y) or Money(currency, value), because any "modification" can just construct a new instance manually. But in some cases, it's more convenient to have the copying implicit, rather than needing a whole set of withFoo methods. Note that you could have COW structs with optional immutability, and get the best of both worlds: struct Foo { readonly int $one; int $two; } $foo = new Foo(1,2); $bar = $foo; // implicit copy-on-write clone $bar->two = 3; // sets the property on $bar, leaving $foo untouched $bar->one = 42; // error I'm not sure how to do this with only reference types plus readonly properties; you'd need some kind of "partial clone": class Foo { readonly int $one; readonly int $two; } $foo = new Foo(1,2); $bar = clone $foo { two => 3 }; // need to set the new property value while constructing, because it's readonly $bar->two = 3; // error $bar->one = 42; // error Regards, -- Rowan Collins [IMSoP]