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: Alastair Houghton <email@hidden>
- Date: Thu, 3 Jun 2004 15:44:16 +0100
On 3 Jun 2004, at 15:17, Ondra Cada wrote:
>
Alastair,
>
>
On 3.6.2004, at 15:49, Alastair Houghton wrote:
>
>
> Are you certain that this is a bug?
>
>
Sure I am. Among others, since...
As you may have gathered, I'm not convinced. I think it makes sense to
do a shallow copy.
>
> I don't see where in the documentation for NSCopying it says that the
>
> copy is necessarily a deep one, and it seems to make more sense in
>
> most cases for a collection class's -copy to perform a shallow copy,
>
> especially for the very common case where a collection is used to
>
> hold a set of immutable objects.
>
>
... in this case a shallow copy is *the very same* operation as a deep
>
copy, thanks to the fact an immutable object, of course, is not
>
actually copied!
Well... that's true, although you might have a *notionally* immutable
object, rather than one that is actually immutable---e.g., an
NSMutableString that was returned via a method with an NSString *
result.
Here are a few more reasons that I think a shallow copy makes more
sense:
1. Deep copy is almost always an unnecessary expense. For the few
cases where a deep copy would be appropriate, you would take a hit on
*many* cases where it wasn't actually necessary.
2. Deep copying goes against the idea that collections are a collection
of references rather than a collection of objects. Yet the
collection-of-references point of view corresponds better (to my mind)
to what is actually going on in the system, and doesn't introduce
confusion over the possibility of modifying a mutable object held in an
immutable array (for instance).
3. The mutability applies to the array, not to the objects in the
array; this is also true of immutable arrays... they can contain
mutable objects, which can be modified if that is what you want to do.
Granted, there are issues with dictionaries (as regards keys) and sets,
but those are fairly obvious to anyone with even an elementary
understanding of the kinds of data structures that might be used to
implement such a collection. They are also well documented.
4. It is difficult (and costly in terms of performance) to implement a
generic deep copy, because of the possibility of circularities or even
just several references to the same object in the data structure. On
the other hand, it is usually easy to implement a deep copy in any
specific case that you come up against.
Kind regards,
Alastair.
--
http://www.alastairs-place.net
[demime 0.98b removed an attachment of type application/pkcs7-signature which had a name of smime.p7s]
_______________________________________________
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.