• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Is that really a bug at all? (was: Re: Ugly bug in Foundation, beware!)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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 18:35:39 +0100

On 3 Jun 2004, at 17:19, Brent Gulanowski wrote:

> On Jun 3, 2004, at 10:44 AM, Alastair Houghton wrote:
>
>> 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.
>
> So you're suggesting that the receiver of said string, expecting an
> immutable object, would then start sending it mutable messages? And
> when it did not complain ... ? Sorry I don't really understand the
> implication...

Ondra was making the point that -copy for an immutable object generally
just -retains it, and in reply I was pointing-out that there are plenty
of objects that are notionally immutable (in that you shouldn't attempt
to change them), despite actually being mutable.

>> 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.
>
> But isn't it enough to have and use -arrayWithArray: (or
> dictionaryWithDictionary: etc.) as Ondra said? Why have two methods
> that do virtually the same thing?

Well that's a very good (and separate) question. I never saw a reason
for the presence of -arrayWithArray:, -dictionaryWithDictionary:,
because -copy and -mutableCopy seem to do the same thing and are
shorter and easier to understand.

>> 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).
>
> Uh, so where is the collection of objects, then?

On the heap.

> I use Arrays as collections of objects.

They're collections of references to objects. You can see this because
you can put a reference to the same object into more than one array,
for instance:

NSMutableArray *a = [NSMutableArray array];
NSMutableArray *b = [NSMutableArray array];
NSString *myStringObject = [NSString stringWithFormat:@"%x", 12345];

[a addObject:myStringObject];
[b addObject:myStringObject];

if ([a lastObject] == [b lastObject]) {
NSLog (@"The two references point to the same object");
}

You can also hold references to the objects that are in your arrays in
pointers or in other collections. The point is that the array contains
*references* not objects.

I think at least part of the original confusion stems from the fact
that the mutability or immutability of a collection object has nothing
whatsoever to do with the mutability (or otherwise) of its contents.
If it did, then it *might* make sense for -copy on an immutable array
to do a shallow copy whilst -copy on a mutable array did a deep copy,
but it does not. The only things that are immutable in an immutable
array are the references and the number of references.

Indeed, if -copy did a deep copy, there would be little point in
*having* immutable collection objects, because one of the largest
optimisations that immutability allows is treating -copy as a -retain
operation, and you would, of necessity, lose that optimisation, because
NSArray would have to duplicate all of the (possibly mutable) objects
it currently referenced.

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.


  • Follow-Ups:
    • Re: Is that really a bug at all? (was: Re: Ugly bug in Foundation, beware!)
      • From: Ondra Cada <email@hidden>
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>)

  • Prev by Date: Re: Feeding NSUnarchiver bad data can cause crash
  • Next by Date: Re: Debugging under 10.2
  • Previous by thread: Re: Is that really a bug at all? (was: Re: Ugly bug in Foundation, beware!)
  • Next by thread: Re: Is that really a bug at all? (was: Re: Ugly bug in Foundation, beware!)
  • Index(es):
    • Date
    • Thread