[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: NSMutableArray copy does a deep copy
From: |
James Knight |
Subject: |
Re: NSMutableArray copy does a deep copy |
Date: |
Wed, 15 Jan 2003 16:28:05 -0500 |
On Tuesday, January 14, 2003, at 06:29 PM, James Knight wrote:
For some reason, NSMutableArray's copyWithZone does a deep copy of the
array, calling copy on all it's elements. NSArray does not do this.
This makes absolutely no sense at all: the mutability of the array
itself has nothing to do with the necessity of copying its elements.
If a deep copy is desired, perhaps a deepCopy method would be useful,
but it's easy enough to implement in code which needs it.
The same problem occurs with NSMutableDictionary and NSCountedSet.
As a point of reference, MacOSX's implementation does a shallow copy
on both classes. Thus, it would seem to me that the current
implementation is both incorrect and non-sensical. Is there some
reason for it being that way?
Okay, I know - bad form answering your own question, but, yes, the
gnustep implementation is almost correct based on the OpenStep spec
from 1994. And yes, the spec'd behavior is nonsensical. And yes, MacOSX
changed the behavior.
If you wanted to make gnustep actually be correct to the OS spec,
copyWithZone on an immutable NSArray should *also* do a deep copy.
According to the spec, "copyWithZone" should always do a deep copy and
"mutableCopyWithZone" should always do a shallow copy. Thus the
optimization that "copyWithZone" on a immutable NSArray returns itself
is incorrect.
http://www.gnustep.org/resources/OpenStepSpec/FoundationKit/Protocols/
NSCopying.html
http://www.gnustep.org/resources/OpenStepSpec/FoundationKit/Protocols/
NSMutableCopying.html
__**HOWEVER**__, since the spec'd behavior is clearly nonsensical, I
think would be a very poor idea to continue following it. The idea that
the mutability of the object has anything to do with whether you want a
deep or shallow copy is just silly.
I'll note that apple did in fact change this behavior on the way to
MacOSX 10.0:
If you go to
http://developer.apple.com/techpubs/macosx/ReleaseNotes/Foundation.html
and go to section heading "Copying Collections Clarified", they have an
explanation of how it used to work:
"In some sense, the immutable copy is a deep copy, and the mutable
copy is a shallow copy. It was, however, a historical mistake to tie
these two concepts together."
As well as what they changed:
"
Copying Collections
Copying semantics of the concrete subclasses of the abstract NS
collection classes has been modified slightly. Now that immutable
collections are again being created, a historical optimization has been
restored. When copyWithZone: is sent to an immutable collection, the
receiver is simply retained and returned. Also, when copyWithZone: is
sent to a mutable collection, the contained objects are just retained
by the new collection (shallow copying), not copied.
-mutableCopyWithZone: also just retains the contained objects in the
new collection, as in the abstract implementation of the method."
James