4 ms·
> not only integers and floats, but also strings, lists and dictionaries as mutable value types (copy-on-write) Lists and dictionaries are also mutable in Pyth
by diarrhea 5y ago
> not only integers and floats, but also strings, lists and dictionaries as mutable value types (copy-on-write)
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.
- withinboredom 5y agoWith copy-on-write: $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).
- deleted 5y ago[deleted]
- kaba0 5y agoSo basically what FP languages do?
- withinboredom 5y agoSurprising php has a lot of FP aspects.
- pas 5y agoIt .. really doesn't. (Sure, probably a tiny bit more than Python.) 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.
- withinboredom 5y agoFWIW, I don't consider type safety a major issue with FP vs. OOP vs. procedural. I think type safety is just that, a safety net. I've ported things between C#, Scala, and PHP for fun, and regularly find PHP to be more concise due to it's lack of type safety than it being a hinderance. Sure, it sucks that the IDE can't help me out more (lack of generics cough) but static analysis tools really help there, like Psalm and PHPStan. As a side note, Psalm's security scans have pointed out security issues with the original code it was ported from, so that's something... You can do some pretty fancy things with arrays that you usually only see in FP type languages: $arr1 + $arr2 (non-numeric keys) // scala: arr1 ++ arr2 array_merge($arr1, $arr2) // scala: (arr1 ++ arr2) distinct array_unique(array_merge($arr1, $arr2)) // scala: arr1 union arr2 array_intersect($arr1, $arr2) // scala: arr1 intersect arr2 array_combine($arr1, $arr2) // scala: arr1 zip arr2 array_filter(array_map($mapper(...), $arr)) // scala: arr.flatmap(mapper) in all of these cases, the original array(s) remains untouched. In most OOP languages, the array would probably be mutable (or require some juggling between mutable and immutable types, like C#). That's why its surprising. It's quite verbose in PHP, but the foundations for a FP approach is all there. What's even more surprising is when performance is comparable to a language like C# on some random benchmarks I've run[1] (opcache + JIT turned on in PHP, C# in "release" optimized compilation). I'd consider it a pretty good contender for serious things. [1]: https://github.com/withinboredom/distributed-hashmap/blob/23baf54213ab3dd473724999b88585bba815f74e/README.md#performance https://github.com/withinboredom/distributed-hashmap/blob/23...
- eyelidlessness 5y agoDepends on the language. Many FP languages use persistent data structures, sharing some memory with derived/updated values.
- TazeTSchnitzel 5y agoWell, what PHP does similarly avoids the risk of accidentally mutating shared values, but it's not the same: • the value is locally mutable • no duplication happens when you want to modify it and there is only a single reference
- synergy20 5y agoso is object(array,map,etc) in javascript, many scripting languages have non-primitives mutable, the OP seems wrong on this claim(e.g. only PHP does that)
- TazeTSchnitzel 5y agoThey aren't value types in JavaScript or Python, they're reference types (as are all types in those languages). Mutable value types are different from both mutable reference types (Python list, JS Array, strings in both languages, etc) and immutable reference types (Python and JS strings, Python tuples…)
- fooyc 5y agoPretty sure they meant immutable in that quote.
- TazeTSchnitzel 5y agoNo, I didn't! PHP's string and array types are neither immutable nor a reference type. In this way they are very different from what Python, JavaScript etc do.