Re: Is that really a bug at all? (was: Re: Ugly bug in Foundation, beware!)
Re: Is that really a bug at all? (was: Re: Ugly bug in Foundation, beware!)
- Subject: Re: Is that really a bug at all? (was: Re: Ugly bug in Foundation, beware!)
- From: Marcel Weiher <email@hidden>
- Date: Sat, 5 Jun 2004 16:00:10 +0200
On 4 Jun 2004, at 19:59, Louis C. Sacha wrote:
The collection classes in Cocoa are defined to work in a specific way,
and have a relatively clear "contract" regarding how they work. While
some people disagree with how NSDictionary and NSArray are
implemented, the way that copy is implemented for those classes is
"correct" in terms of their "contracts".
That just means that they documented it correctly, not that it isn't
broken. As I've said before, NSDictionary's copying the key is a
*design* bug, not an implementation bug.
Here's a somewhat more detailed analysis on why NSDictionary's behavior
of copying the key is wrong as implemented.
First, there is *nothing* whatsoever in the behavior of a dictionary,
in general CompSci terms, that says its keys should be copied. A
dictionary is simply a mapping of keys to values. I have never seen
either a definition of dictionary or an implementation that does the
copying (apart from this one).
Second, the definition is a mapping of (object) keys to values. NOT of
the key's value to a value. Since one of the fundamental properties
of objects is that they have identity (See RM-ODP and other definitions
of the term), the fact that a *different* object is actually used as a
key is, at the very least, an odd choice. It certainly means that
NSDictionary does not, in fact, implement a dictionary, just something
that is sort of similar most of the time.
The reason given is that the key shouldn't change, because otherwise
the implementation will break. However (a) this could simply be stated
in the contract and (b) copying DOESN'T SOLVE THE PROBLEM, as should be
abundantly clear by now, because making a copy in no way guarantees
that they copy will not be changed.
Furthermore, copying-behavior can easily be added (a) by the client,
(b) using a new method in a category or (c) using a subclass, when and
if it is desired. However, non-copying behavior cannot
easily/efficiently be implemented on top of the oopying behavior. You
either have to re-implement the whole dictionary or create a helper
key-object that holds on to the real key and short-circuits the copy
with a retain (or does a shallow copy). I have done both, neither is
pleasant, and neither should be necessary.
Cheers,
Marcel
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
References: | |
| >Ugly bug in Foundation, beware! (From: Ondra Cada <email@hidden>) |
| >Is that really a bug at all? (was: Re: Ugly bug in Foundation, beware!) (From: Alastair Houghton <email@hidden>) |
| >Re: Is that really a bug at all? (was: Re: Ugly bug in Foundation, beware!) (From: Ondra Cada <email@hidden>) |
| >Re: Is that really a bug at all? (was: Re: Ugly bug in Foundation, beware!) (From: Alastair Houghton <email@hidden>) |
| >Re: Is that really a bug at all? (was: Re: Ugly bug in Foundation, beware!) (From: Brent Gulanowski <email@hidden>) |
| >Re: Is that really a bug at all? (was: Re: Ugly bug in Foundation, beware!) (From: Alastair Houghton <email@hidden>) |
| >Re: Is that really a bug at all? (was: Re: Ugly bug in Foundation, beware!) (From: Ondra Cada <email@hidden>) |
| >Re: Is that really a bug at all? (was: Re: Ugly bug in Foundation, beware!) (From: Marcel Weiher <email@hidden>) |
| >Re: Is that really a bug at all? (was: Re: Ugly bug in Foundation, beware!) (From: Brent Gulanowski <email@hidden>) |
| >Re: Is that really a bug at all? (was: Re: Ugly bug in Foundation, beware!) (From: "Louis C. Sacha" <email@hidden>) |