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