On Thu, Dec 31, 2020, at 8:04 AM, Olle Härstedt wrote: > 2020-12-31 12:37 GMT, Rowan Tommins <rowan.coll...@gmail.com>:
> > On 30/12/2020 18:42, Olle Härstedt wrote: > >>> To put it a different way, value types naturally form*expressions*, > >>> which mutable objects model clumsily. It would be very tedious if we had > >>> to avoid accidentally mutating the speed of light: > >>> > >>> $e = (clone $m) * ((clone $c) ** 2); > >> Using a variable on right-hand side does not automatically create an > >> alias, so in the above case you don't have to use clone. > > > > > > Whether or not the type system forced you to, you'd have to use clone if > > the values were implemented as mutable. Switching to methods again may > > make that clearer: > > > > $c = new MyNumber(299_792_458); > > $m = new MyNumber(10); > > $e = $m->multiply( $c->square() ); > > > > If multiply() and square() are mutating state, rather than returning new > > instances, $c is now 89875517873681764, which is going to totally mess > > up the universe... > > > > > > Regards, > > > > -- > > Rowan Tommins > > [IMSoP] > > Yes, of course you can find use-cases where immutability is a better > choice, just like I can find use-cases where (constrained) mutability > is better. The point is not to replace one tool with another, but > rather adding another tool to the toolbox. The web dev discourse is > one-sided with regard to immutability, I think. Wish I had time to > implement a PR to Psalm to show something more concrete... Again, if > you only have a hammer, everything looks like a nail. :) > > Olle > The web dev discourse is > one-sided with regard to immutability, Yes, if you've heard any of the regular whining about PSR-7 being an immutable object you'd think it's one-sided in favor of mutability. ;-) As you say, the point here is to add tools. Right now, doing immutability in PHP in syntactically clumsy and ugly. We want to fix that, and that has to include some means of "give me a new value based on this existing value but with some difference." (aka, exactly what with-er methods do, although I agree entirely that if you have the option of less generic names, use them). So, can we get back to the original post, which is proposing specifics of the tools to make that happen? :-) (Asymmetric visibility and clone-with, specifically.) --Larry Garfield -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: https://www.php.net/unsub.php