Given that mutable structs are generally regarded as evil (e.g., Why are mutable structs “evil”?), are there potential benefits that might have prompted the designers of the .NET framework to make System.Windows.Point
& System.Windows.Vector
mutable?
I'd like to understand this so I can decide whether it would make sense to make my own similar structs mutable (if ever). It's possible the decision to make Point
and Vector
mutable was just an error in judgment, but if there was a good reason (e.g., a performance benefit), I'd like to understand what it was.
I know that I've stumbled over the implementation of the Vector.Normalize()
method a few times because it, surprise (!), does not return a fresh Vector
. It just alters the current vector.
I always think it should work like this:
var vector = new Vector(7, 11);
var normalizedVector = vector.Normalize(); // Bzzz! Won't compile
But it actually works like this:
var vector = new Vector(7, 11);
vector.Normalize(); // This compiles, but now I've overwritten my original vector
...so, it seems like immutability is a good idea simply for avoiding confusion, but again, perhaps it's worth that potential confusion in some cases.
Such types are mutable because, contrary to what some people might claim, mutable value-type semantics are useful. There are a few places where .net tries to pretend that value types should have the same semantics as reference types. Since mutable value-type semantics are fundamentally different from mutable reference-type semantics, pretending they're the same will cause problems. That doesn't make them "evil", however--it merely shows a flaw in an object model which assumes that acting upon a copy of something will be semantically equivalent to acting upon the original. True if the thing in question is an object reference; generally true--but with exceptions--if it's an immutable structure; false if it's a mutable structure.
One of the beautiful things about structs with exposed fields is that their semantics are readily ascertained by even simple inspection. If one has a
Point[100] PointArray
, one has 100 distinct instances ofPoint
. If one saysPointArray[4].X = 9;
, that will change one item ofPointArray
and no other.Suppose instead of using struct
Point
, one had a mutable classPointClass
:How many PointClass instances are stored in
PointClass[100] PointClassArray
? Is there any way to tell? Will the statementPointClass[4].X = 9
affect the value of PointClass[2].X? What aboutsomeOtherObject.somePoint.X
?While the .net collections are not well suited to storage of mutable structs, I would nonetheless regard:
to have relatively clear semantics, at least in the absence of threading issues. While I consider it unfortunate that .net collections don't provide a means by which one could simply say
myDict[lookupKey].X = 9;
I would still regard the above code as pretty clear and self-explanatory without having to know anything about Point other than the fact that it has a public integer field called X. By contrast, if one had aDictionary<PointClass>
, it would be unclear what one should be expected to do to change the X value associated with "George". Perhaps thePointClass
instance associated with George is not used anywhere else, in which case one may simply write the appropriate field. On the other hand, it's also possible that someone else has grabbed a copy ofMyDict["George"]
for the purpose of capturing the values therein, and isn't expecting that thePointClass
object he's grabbed might change.Some people might think "Point" should be an immutable struct, but the effect of a statement like
somePoint.X = 5;
can be fully determined knowing only thatsomePoint
is a variable of typePoint
, which in turn is a struct with a public int field calledX
. IfPoint
were an immutable struct, one would have to instead say something likesomePoint = new Point(5, somePoint.Y);
, which would, in addition to being slower, require examining the struct to determine that all of its fields are initialized in the constructor, with X being the first and Y the second. In what sense would that be an improvement oversomePoint.X = 5;
?BTW, the biggest 'gotcha' with mutable structs stems from the fact that there's no way for the system to distinguish struct methods which alter 'this' from those which do not. A major shame. The preferred workarounds are either to use functions which return new structs derived from old ones, or else use static functions which accept "ref" struct parameters.
Possibilities:
List<T>.Enumerator
is a mutable struct that was used because it seemed like a good idea at the time to take advantage of the micro-opts that would often happen. It's almost the poster-child for mutable structs being "evil" as it's bitten more than a few people. Still, it seemed like a good idea to someone at the time...These types are in the
System.Windows
namespace and are generally used in WPF applications. The XAML markup of an application is a big part of the framework so for a lot of things, they need a way to be expressed using XAML. Unfortunately there's no way to invoke non-parameterless constructors using WPF XAML (but it is possible in loose XAML) so trying to call a constructor with the appropriate arguments to initialize it wouldn't be possible. You can only set the values of the object's properties so naturally, these properties needed to be mutable.Is this a bad thing? For these types, I'd say no. They are just for holding data, nothing more. If you wanted to get the size a
Window
wanted to be, you'd access theDesiredSize
to get theSize
object representing the size it wanted. You're not meant to "change the desired size" by altering theWidth
orHeight
properties of theSize
object you get, you change the size by providing a newSize
object. Looking at it this way is a lot more natural I believe.If these objects were more complex and did more complicated operations or had state, then yes, you wouldn't want to make these types neither mutable nor structs. However since they're just about as simple and basic as it can get (essentially a POD), structs would be appropriate here.