Normally mutable objects (such as your "Circle" example) are not good
candidates for keys in Hashtables.
I disagree.
The problem that Patrick is facing here is one of semantics. That is,
what exactly is his hypothetical circle object in business terms, and
what does that imply about its identity, and about hashing it.
I see three possibilities:
1. The object (such as a circle) is what I would consider a value type.
Talking about a particular instance as having an identity different
from other instances does not make sense. For example, talking about
"this particular integer value 5" versus "that integer value 5 over
there" doesn't ring true. The value 5 is the value 5, and it makes no
sense to put it in a hash table. One could design a circle in the same
way: "this is a circle of radius 5, and it makes no sense to talk about
'this circle of radius 5' versus 'that circle of radius 5'." A value
type is typically a property of some other thing that has an identity;
it has no identity of its own, so you would hash on the identity of the
owner object.
2. The object has a unique identity. For example, a particular circle
that the user has drawn at particular coordinates with a partiuclar
colour. This is a case in which you want to assign the object a UID
that has nothing to do with its radius, location, or colour, because no
matter what happens to the circle, you want to clearly distinguish
between this circle and that other circle. Even if the user drew them
at the same location with the same radius and same colour, thay are
distinct objects.
3. You may wish to hash the second type of circle (the one with a clear
identity) by location, or some such thing, for lookup purposes. It is
in this case that you are hashing on mutable properties, and you need
to adopt a smart system such as the one that Jay outlined in order to
manage the lookup table as the circle's properties change. However, the
hash table is a convenience measure, not a hash on identity, and there
may be other ways to get the same effect without the complexity of
managing mutable hash keys.
So, you have to ask yourself:
1. Does the object I'm working with have an identity? Does it make
sense to distinguish between two otherwise identical objects because
they represent real world things that are dinstinct even if all of
their qualities are the same?
2. Am I hashing on identity, or as a convenient lookup on some quality
of the object that may change? If it's the latter, is there another way
to get the same convenience without having to worry about hashing on
mutable keys? If it's the former, and the object has no obviously
unique, immutable data on which to base a hash key, then you probably
need a UID.