3 ms·
"Ok, but then is there a reason to have immutability with collections that aren't shared by multiple threads?" I think so. What about this scenario: Poin
by fogus 17y ago
"Ok, but then is there a reason to have immutability with collections that aren't shared by multiple threads?"
I think so. What about this scenario:
Point foo = new Point(10, 0);
frob(foo);
zap(foo);
What's the value of foo.x? foo.y?
I have no idea.
In Clojure:
(def foo {:x 10, :y 0})
(frob foo)
(zap foo)
What's the value of foo? {:x 10, :y 0}
That certainty is valuable.
- runT1ME 17y agoYou make an interesting point, one I hadn't considered, which is code readability. It certainly is easier to read. That being said, you could still take a look at what frob and zap do, and figure out what the value of foo was, assuming it was thread local/not shared.