2021-01-02 21:25 GMT, Larry Garfield <la...@garfieldtech.com>: > On Sat, Jan 2, 2021, at 12:56 PM, Rowan Tommins wrote: >> On 01/01/2021 20:31, Paul M. Jones wrote: >> > The complaints against the incomplete and inconsistent immutability of >> > PSR-7 have merit. >> >> >> The big mistake of PSR-7, in my view, is mixing immutable objects with >> streams, which are inherently mutable/stateful. I wonder if there are >> any lessons to be learned there in terms of what kinds of immutability >> the language should encourage / make easy. >> >> For instance, is it better to constrain entire objects to be immutable >> rather than individual properties? And should there be restrictions on >> what you can declare as immutable, since "immutable resource" is largely >> nonsensical? >> >> Or is it rather a reflection that building purely immutable >> implementations is hard, and the language needs other facilities >> (monads? ownership?) to mix in those parts that don't fit? >> >> Regards, > > I rarely hear that called out as a PSR-7 complaint specifically, in > practice, but moving on... > > IMO, it's better to put the focus on immutable properties. There are use > cases where you want only some properties to be immutable, but not the whole > class. If you do want the whole class, then marking all the properties > immutable is effectively the same thing. > > Though, again, in practice, at least in PHP, I don't think immutable > properties should be the goal. Asymmetric visibility lets us built safely > immutable-from-the-outside objects that are "up to you" on the inside. I > think that gives us a better end result given the nature of PHP. In other > languages that wouldn't make as much sense, but PHP is what it is. > > Copy on write makes "immutable data structures" not something we need to > build explicitly; we get "close enough" for free. If you really wanted to > micro-optimize the memory and CPU usage further than that... go build it in > Rust instead.
Correct me if I'm wrong, but copy-on-write is only beneficial with values, not references to values (objects)? When you clone with a `with` method, you always write, so you always have to copy. And objects are already free to pass around without any copying happening automatically (as is the case with arrays, which have value semantics, which is why copy-on-write was implemented to not copy needlessly when an array is only read from). Olle -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: https://www.php.net/unsub.php