by diarrhea · 4y ago · 0 comments
Lists and dictionaries are also mutable in Python. Strings are not. With CoW, are your referring to garbage collection? (value that is no longer assigned to a name will eventually be purged). That would be independent of (im)mutability, so I'm not sure I understand.
$a = ['a'];
$b = $a;
$a and $b both point to the exact same bit of memory. Thus passing an array between functions is zero-cost.When you do:
$b[] = 'b'; // append 'b'
a new array is allocated, the old data copied to it, and 'b' appended to it (basically, but IIRC, it is a bit more optimized than that).• the value is locally mutable
• no duplication happens when you want to modify it and there is only a single reference
It's not static types FP like Haskell/Scala (or even TS), as it just recently started gaining some type safety, and while it had map/reduce call_user_func, it wasn't that big in PHP world.
It simply has/had too much mutability for it, and most PHP practice focused on trying to implement OOP ideas in it in a nice way.