• 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: Charles Srstka <email@hidden>
  • Date: Tue, 10 Jun 2008 10:28:13 -0500

On Jun 9, 2008, at 5:55 PM, Hamish Allan wrote:

Sure. But it gives you *more* information than if it just returns
"id". I agree with you in all other respects of your post, but I don't
agree that +[NSArray array] returns "id" because if it returned
"NSArray *" you'd have to have a separate declaration for
+[NSMutableArray array]. Indeed, that line of reasoning leads to
John's way of thinking: that if you return something more specific
than "id", it is in some sense indicative that the returned object is
more likely to be of a specific class, rather than a subclass thereof.

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:


NSMutableArray *array = [NSMutableArray array];

The only way to get the compiler warnings to shut up would be to cast it. If the methods return id, then the compiler just assumes you're doing the right thing (which also means you can do stupid things like assign +[NSMutableArray array] to an NSString or something which would of course end up blowing up before too long, so it's not perfect either, but it's certainly less annoying than having to typecast every assignment of a newly-initialized NSMutableArray).

Charles
_______________________________________________

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


  • Follow-Ups:
    • Re: Garbage collector vs variable lifetime
      • From: John Engelhart <email@hidden>
    • Re: Garbage collector vs variable lifetime
      • From: "Hamish Allan" <email@hidden>
    • Re: Garbage collector vs variable lifetime
      • From: "Adam R. Maxwell" <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>)

  • Prev by Date: Re: checking application is document based application or not
  • Next by Date: Re: Building against 10.5 SDK, link error on "open"
  • Previous by thread: Re: Garbage collector vs variable lifetime
  • Next by thread: Re: Garbage collector vs variable lifetime
  • Index(es):
    • Date
    • Thread