Re: When do I need to override hash?
Re: When do I need to override hash?
- Subject: Re: When do I need to override hash?
- From: Quincey Morris <email@hidden>
- Date: Thu, 20 Aug 2009 22:48:04 -0700
On Aug 20, 2009, at 22:05, Jeff Laing wrote:
Without wanting to keep the thread going forever, can I just ask why
we would presume this?
In fact, if I were implementing NSDictionary I'd assume the reverse,
that I was not allowed to assume that an objects hash would not
change.
Is there some documentation on this restriction on the types of
objects that can be put into an NSDictionary?
Seth explicitly quoted the documentation of this restriction in the
post to which I replied:
(This is from the documentation for 'hash'.)
"If a mutable object is added to a collection that uses hash values
to determine the object’s position in the collection, the value
returned by the hash method of the object must not change while the
object is in the collection."
It is absolutely possible that NSDictionary does not use hash values
"to determine the object's position in the collection" and so would be
exempt from the above restriction.
It's also possible (maybe even likely) that NSArray doesn't use hash
values "to determine the object's position in the collection".
So, now that you mention it, I guess we don't know for sure which
collections do and do not depend on hash values for this purpose.
(It's certainly conceivable that both NSDictionary and NSArray do use
object hash values to manage their internal storage, perhaps only for
very large collections.) Sounds like something that ought to be
documented explicitly somewhere.
In practice, it's not of general concern, because it doesn't seem
generally useful to have objects that are safely collectable only in
certain kinds of collections. For those specific cases where you had
mutable objects with "unstable" hash values, you'd have to choose and
bet on your interpretation of which collections it was safe to use.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden