Hacker News new | ask | show | jobs
by PaulHoule 1838 days ago
Immutability can be good for complex data structures that you'd like to have multiple versions of.

For instance in a chess program you might be traversing a tree and decide you want to go back a few moves and with persistent data structures the original data structure is still there and shares a lot of memory with the current board state.

Oddly though last time I wrote a chess program it used Markov Chain Monte Carlo and to do that you always run playouts forwards so there is no advantage to persistent data structures in the playouts even though it might be useful for keeping counts of how many times you won after playing out different moves.

In Java half-understanding of "immutability" can be dangerous. For instance, that persistent list is really immutable of the member objects are immutable. However, you can't write

   final PersistentList myList;
if you ever want to add or remove to the list (you need to replace the reference with a new list.)

   final List myList;
on the other hand cannot be replaced with a new list but you can add and remove items from it as much you like.

   final ImmutableList myList;
won't let you add or remove anything or change the reference, but if the members are mutable nothing stops you from changing them.

Thus it is easy to get in trouble if you have been taught maxims like "final is good", "immutable is good", etc.