[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: the benefits of immutability

Michael St . Hippolyte wrote:
 > the real culprit is not inheritance but mutability.  This is also
 > true of the various Circle examples in this thread.  I would be
 > curious to see if anyone can come up with a similar example showing
 > broken behavior when functional restrictions are in place,
 > i.e. objects are immutable and methods are side-effect free.

What kind of functional restrictions do you have in mind?  The classes
used earlier in this thread -- Circle and ColoredCircle -- are
immutable, for some fairly reasonable definition of "immutable".  All
of their methods are side-effect free (or can be made side-effect free
without any bearing on the equivalence problem).

    public class Circle {
       private int radius;

       public Circle(int radius) {
           this.radius = radius;

       public final int getRadius() {
           return radius;

       // public boolean equals(Object obj);

    public final class ColoredCircle extends Circle {
       private Color color;

       public ColoredCircle(int radius, Color color) {
           this.color = color;

       public final Color getColor() {
           return color;

       // public boolean equals(Object obj);

If these aren't immutable, then I don't know what is.