September 2014

Sun Mon Tue Wed Thu Fri Sat
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30        








« Debugging an OpenGL ES 2.0 application using a virtual Android device | Main | YASL? »

October 19, 2012

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. :-)

blog comments powered by Disqus

Feed/Share

10 Random Posts