Hi,

Chris Riley wrote:

There has been a lot of interest recently (eg psr-7) in immutable data. I'm
considering putting an RFC together to add language support for immutables:


I wonder if immutable classes are really the right way to go. Immutable reference types with manual copying are somewhat alien to PHP: instead, the language has used copy-on-write mutable value types for arrays and strings, and prior to PHP 5, objects.

Copy-on-write value types have all the benefits immutable types do. They can't be mutated at a distance unless you make a reference to them, trees made of them can be compared for equality with just ===, etc.

But they also have benefits that immutable types lack. They're easier to work with, because you can directly mutate them like everyone's used to doing: no messing around with withFooBar() and withoutFooBar(), you just set foobar or remove it directly. And PHP handles the duplication of objects for you implicitly, so there's far less code to write.

Unfortunately, PHP 5 got rid of PHP 4's value type objects and replaced them with reference type objects. But we could always introduce a way to get value type objects again. How about a `struct` keyword? It would be equivalent to `class`, but produce a value type rather than a reference type.

Any thoughts?

Thanks.
--
Andrea Faulds
http://ajf.me/

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to