• 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: Garbage collector vs variable lifetime
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Garbage collector vs variable lifetime


  • Subject: Re: Garbage collector vs variable lifetime
  • From: Jean-Daniel Dupas <email@hidden>
  • Date: Wed, 11 Jun 2008 09:41:35 +0200


Le 11 juin 08 à 09:01, John Engelhart a écrit :


On Jun 10, 2008, at 11:28 AM, Charles Srstka wrote:

I think the problem is that if NSArray has +[NSArray array] returning an NSArray, then NSMutableArray has to return an NSArray also, since it can't have a different method signature for the same method. As a result, if you called +[NSMutableArray array], the compiler would think you were getting a regular, non-mutable NSArray, and you'd get a warning if you tried to do this, although the code would still work:

No, each class is allowed to define different types for the same method:


http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/Articles/chapter_9_section_2.html#/ /apple_ref/doc/uid/TP30001163-CH16-TPXREF161

This whole business about '(NSArray *) means NSArray AND any of it's subclasses' is the result of sloppy thinking and confusing 'able to' with 'as per spec'.

If you declare a method prototype as '-(NSArray *)resultsArray', then you have explicitly communicated that a NSArray is going to be returned. Not a NSMutableArray. Not 'Jimmies groovy array with red pin stripes'. A NSArray. Period. A NSMutableArray != a NSArray. If you're going to be returning (or accepting) more than a single class, you use id, which clearly communicates your intentions.

A return type of NSArray * means something very explicit. "But all you can expect is an object that behaves like a NSArray, so returning a subclass of NSArray, like NSMutableArray, is perfectly legal!" You're exactly right that all I can expect is an object that behave like a NSArray. /Exactly/ like a NSArray. Not sorta. Not almost. Exactly. Because that's what you explicitly stated. So when the returned array mutates, either because you were sloppy and returned the pointer to your objects internal state, or because during its travels something, somewhere sends it a mutation message inside a @try / @catch block, I'm going to file a bug because whatever it is you returned did not behave like a NSArray. An / immutable/ array.

You have to fill a bug againt the library that mutates the NSArray, not the one that create it. If a method expects an NSArray, it have to use it like an NSArray and not like an NSMutableArray.


Attachment: smime.p7s
Description: S/MIME cryptographic signature

_______________________________________________

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

References: 
 >Garbage collector vs variable lifetime (From: Quincey Morris <email@hidden>)
 >Re: Garbage collector vs variable lifetime (From: Chris Hanson <email@hidden>)
 >Re: Garbage collector vs variable lifetime (From: John Engelhart <email@hidden>)
 >Re: Garbage collector vs variable lifetime (From: Chris Hanson <email@hidden>)
 >Re: Garbage collector vs variable lifetime (From: John Engelhart <email@hidden>)
 >Re: Garbage collector vs variable lifetime (From: Chris Hanson <email@hidden>)
 >Re: Garbage collector vs variable lifetime (From: John Engelhart <email@hidden>)
 >Re: Garbage collector vs variable lifetime (From: Chris Hanson <email@hidden>)
 >Re: Garbage collector vs variable lifetime (From: "Hamish Allan" <email@hidden>)
 >Re: Garbage collector vs variable lifetime (From: Chris Hanson <email@hidden>)
 >Re: Garbage collector vs variable lifetime (From: "Hamish Allan" <email@hidden>)
 >Re: Garbage collector vs variable lifetime (From: Charles Srstka <email@hidden>)
 >Re: Garbage collector vs variable lifetime (From: John Engelhart <email@hidden>)

  • Prev by Date: Re: A simple doubt about array
  • Next by Date: Re: creating and using a View xib
  • Previous by thread: Re: Garbage collector vs variable lifetime
  • Next by thread: Re: Garbage collector vs variable lifetime
  • Index(es):
    • Date
    • Thread