Why are Point2d and Point3d objects immutable?

It's been a hectic week and I haven't been able to find much time to work on a final post for today, so I had a quick delve in my "interesting" folder and found this little gem.

Not long ago, someone asked me by email about the reason for making the various co-ordinate properties (X/Y/Z) of Autodesk.AutoCAD.Geometry.Point2d and Point3d read-only, essentially making these classes immutable.

Being a big fan of functional programming, I can think of lots of good reasons for immutability, most of which (and more) have also been given to justify the logic behind making System.String an immutable type over on Stack Overflow.

But not having designed the .NET API in AutoCAD, I wasn't aware of the specific design decision behind it. As it was an internal question – i.e. coming from an Autodesk employee – I suggested they contact the person who did in fact design the API, my good friend and colleague Albert Szilvasy. The response Albert gave (and was passed back to me) was the following:

The same reason why System.String is immutable.

The problem is that a mutable type is confusing to use when it appears as a property return value. For example:

class A

{

  public Point3d prop {get;}

};

 

A a;

a.prop.X = 1.0;

Would this syntax mean that I can set the X value of a read-only property? Or would this simply mean that I set the X value of a temporary point object returned by prop?

So there you have it. 🙂

8 responses to “Why are Point2d and Point3d objects immutable?”

  1. There is talk of changing how C# works with regards to properties with immutable types. And in fact, it does something very similar to that right now, for events.

    tirania.org/blog/archive/2012/Apr-11.html

  2. That's interesting, thanks Tony.

    Is anyone from the C# team talking about this change, out of interest?

    Kean

  3. Well written post, good researched and useful for me in the future.I am so happy you took the time and effort to make this. Best of luck

  4. Im new to the .net;c# and am trying to write a script that modifies x,y coordinates of points in line/poly/....
    does this mean I can not edit position with this command:
    ln.StartPoint.X = ln.StartPoint.X +5; ?
    similiar to what you did with this lisp: adndevblog.typepad.com/autocad/
    ty
    P.S. thank you for your dedication at writing this blog.
    this is my 1st post, and I cant tell you how helpfull you are (been reading it for the past month)

    1. Kean Walmsley Avatar

      Hi Lucano,

      Glad to know it's helpful!

      That is correct: you need to create a new point based on the old one.

      Regards,

      Kean

  5. I know this is very very old - nice explanation - but why struct instead of a class?

    1. I don't know this, off the top of my head, so I suggest posting to the AutoCAD .NET forum.

      Kean

      1. I think I figure out an aswer to this - C# does not allow this

        Class.StructProperty.Property = value;

        But reading Class.StructProperty into a variable like

        v = Class.StructProperty;

        and then

        v.Property = value;

        will compile.

        BUT: StructProperty is a struct, so reading it will simply copy values to a new instance of a StructProperty type Writing to the properties of the new instance won't modify the Class.StructProperty properties.

        In a case of

        Class.ClassProperty.Property = value;

        will compile and will change ClassProperty.Property value since

        Class.ClassProperty is a reference to the ClassProperty object.

Leave a Reply

Your email address will not be published. Required fields are marked *