why doesn't the compiler complain?
why doesn't the compiler complain?
- Subject: why doesn't the compiler complain?
- From: Matt Neuburg <email@hidden>
- Date: Mon, 26 Apr 2010 13:01:58 -0700
- Thread-topic: why doesn't the compiler complain?
I am so confused about something in Objective-C that I thought was perfectly
clear and that I understood perfectly well. Please give me some kind of dope
slap to get my brain back on track.
The Objective-C docs say:
"Methods in different classes that have the same selector (the same name)
must also share the same return and argument types. This constraint is
imposed by the compiler..."
I thought I'd test this assertion, and just the opposite seems to be true:
the compiler isn't imposing any constraints at all. This leaves me confused
about what the rule is.
Here's my test:
@interface MyClass : NSObject {
}
- (void) tryme: (NSString*) s;
@end
@implementation MyClass
- (void) tryme: (NSString*) s {
;
}
@end
@interface MyClass2 : NSObject {
}
- (void) tryme: (NSArray*) s;
@end
@implementation MyClass2
- (void) tryme: (NSArray*) s {
;
}
@end
Those are methods in different classes with the same name but different
argument types, right? Yet that code compiles just fine. What happened to
the constraint imposed by the compiler?
So now let's try actually calling tryme (in yet another class):
MyClass* thing = [[MyClass alloc] init];
NSString* s = @"Howdy";
[(id)thing tryme: s];
This, too, compiles with no problem. Why is the compiler not complaining
that tryme: is ambiguous? Isn't that what it's supposed to do? (That is why
I cast to an id, so that the compiler wouldn't be able to resolve tryme:.)
I must be missing something unbelievably fundamental. What is it? Thx -
m.
--
matt neuburg, phd = email@hidden, <http://www.tidbits.com/matt/>
A fool + a tool + an autorelease pool = cool!
AppleScript: the Definitive Guide - Second Edition!
http://www.tidbits.com/matt/default.html#applescriptthings
_______________________________________________
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