Re: Garbage collector vs variable lifetime
Re: Garbage collector vs variable lifetime
- Subject: Re: Garbage collector vs variable lifetime
- From: "Adam R. Maxwell" <email@hidden>
- Date: Tue, 10 Jun 2008 09:18:21 -0700
On Tuesday, June 10, 2008, at 08:29AM, "Charles Srstka" <email@hidden> wrote:
>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:
I'm not sure if I follow you, but the trivial example below compiles with only one warning. NSMutableArray could redeclare the superclass' implementation of +array, but that would have to be done for each factory method that presently returns an id, which is pretty annoying. In addition, each successive subclass has to redeclare /and/ reimplement all of the factory methods. Another example: if you subclass NSFontManager using setFontManagerFactory:, every time you call +sharedFontManager you have to cast to your subclass since it's strongly typed.
#import <Foundation/Foundation.h>
@interface Thing : NSObject
+ (Thing *)thing;
@end
@interface MutableThing : Thing
+ (MutableThing *)thing;
@end
@implementation Thing
+ (Thing *)thing { return [NSAllocateObject(self, 0, NULL) autorelease]; }
@end
@implementation MutableThing
+ (MutableThing *)thing { return [NSAllocateObject(self, 0, NULL) autorelease]; }
@end
int main (int argc, char const *argv[])
{
NSAutoreleasePool *pool = [NSAutoreleasePool new];
Thing *a = [Thing thing];
a = [MutableThing thing];
MutableThing *b = [MutableThing thing];
// warning: assignment from distinct Objective-C type
b = [Thing thing];
[pool release];
return 0;
}
_______________________________________________
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