Thursday, September 10, 2009

What are Immutable Object

I think every programmer should seriously try to understand what immutable objects/persistent data structures are about and how they can simplify and solve certain problems especially when they have to deal with threading/concurrency since they are inherently thread safe.

This is probably the simplest definition that I found.

immutable
adj. 1. (of an object) Having components that cannot be modified once the object has been created. 2. (of a class) An immutable class is a class all of whose objects are immutable. [annotate]
So what does that mean in code ?

It means that when you do something like

A=(1 2 3) ; where (1 2 3) is an imutable list of integers 1, 2 and 3

We can't say swap the element 2 for a 4 because it is imutable. Some languages allow you to replace the list like

A=(4 5 6), In Erlang you can't once it is assigned you can't change it.

Links:

Java theory and practice - To mutate or not to mutate

Immutable object

No comments:

Post a Comment