• 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: style question: when to use 'id' as return value?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: style question: when to use 'id' as return value?


  • Subject: Re: style question: when to use 'id' as return value?
  • From: glenn andreas <email@hidden>
  • Date: Mon, 17 Dec 2007 17:23:26 -0600


On Dec 17, 2007, at 4:46 PM, William Bumgarner wrote:


On Monday, December 17, 2007, at 02:33PM, "Chris Suter" <email@hidden > wrote:

On 18/12/2007, at 9:25 AM, Bill Bumgarner wrote:

On Dec 17, 2007, at 2:14 PM, Sean McBride wrote:
NSString has the method:

+ (id)stringWithString:(NSString *)aString

Why is the return value not an 'NSString*' instead of an 'id'?  Is
this
just a stylistic preference, or...?

Because both NSString and NSMutableString Do The Right Thing when that method is invoked and, thus, declaring that the method returns a particular type of string -- mutable or immutable -- would be wrong half the time.

NSString is a superclass of NSMutableString so it wouldn't be wrong to
say it returns NSString.

Wouldn't be wrong, but it would be misleading, inconvenient and make an error prone pattern a too convenient habit.


Not that (id) isn't without its problems...


Maybe we need something like

@interface NSString
+ (@class) stringWithString: (NSString *) aString;
@end

or some other way to indicate to the compiler that the return type is an instance of the class that is the receiver (or the same class, or a subclass, as the receiver for instance methods)? Hopefully less ugly than (@class), though...

This would also help clean up

@interface NSObject
+ (@class) alloc;
- (@class) init;
- (@class) autorelease;
@end

So we'd know that the return value is the same subclass of NSObject (or is a subclass of in the case of a class cluster, but that would be implementation detail).

This would catch things like

- (Foo *) doSomething
{
	return [[[Bar alloc] init] autorelease];
}

Since the compiler would know that [Bar alloc] returns an instance of Bar, init will also return an instance of Bar, as will autorelease, and if Bar isn't a subclass of Foo, it's an error.

It would also help to avoid warnings (and the chance to do the wrong thing if they are ignored) with things like

@interface Foo: NSObject
- (id) initWithLength: (int) length;
@end

@interface Bar: NSObject
- (id) initWithLength: (float) length;
@end


Bar *b = [[Bar alloc] initWithLength: 5]; // if we pick the wrong signature, the floating point value is garbage






Glenn Andreas email@hidden
<http://www.gandreas.com/> wicked fun!
quadrium | prime : build, mutate, evolve, animate : the next generation of fractal art




_______________________________________________

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: 
 >style question: when to use 'id' as return value? (From: "Sean McBride" <email@hidden>)
 >Re: style question: when to use 'id' as return value? (From: Bill Bumgarner <email@hidden>)
 >Re: style question: when to use 'id' as return value? (From: Chris Suter <email@hidden>)
 >Re: style question: when to use 'id' as return value? (From: William Bumgarner <email@hidden>)

  • Prev by Date: Re: style question: when to use 'id' as return value?
  • Next by Date: setWantsLayer causes text fields to draw focus ring incorrectly
  • Previous by thread: Re: style question: when to use 'id' as return value?
  • Next by thread: Re: style question: when to use 'id' as return value?
  • Index(es):
    • Date
    • Thread