Re: [Obj-C Compiler Bug?] Two different classes declaring a message with the same name - compiler warning
Re: [Obj-C Compiler Bug?] Two different classes declaring a message with the same name - compiler warning
- Subject: Re: [Obj-C Compiler Bug?] Two different classes declaring a message with the same name - compiler warning
- From: "Oleg Krupnov" <email@hidden>
- Date: Tue, 14 Oct 2008 12:17:14 +0300
You are right, casting from alloc worked, thank you.
Is it the recommended practice to always cast after alloc?
I still have a question in this regard.
If the alloc returns id, then, from the compiler's perspective, there
can be two solutions: 1) assume that the id type can have each and any
method and let it be resolved at run time without any compiler warning
or 2) assume that the id does not have any methods (except NSObject's)
and always issue a compiler warning when there is no explicit casting
to a type. What compiler seems to be doing is rather strange: it looks
through the project, arbitrarily picks up a class with the matching
message name and issues a warning if the rest of the actual message
signature is not matching. How would you explain that?
On Tue, Oct 14, 2008 at 12:00 PM, Chris Suter <email@hidden> wrote:
> On Tue, Oct 14, 2008 at 7:43 PM, Oleg Krupnov <email@hidden> wrote:
>> In my project I have two different, totally unrelated classes.
>>
>> @interface ClassA : NSObject{}
>>
>> -(id)initWithContext:(ContextA*)context;
>>
>> @end
>>
>>
>> @interface ClassB : NSObject{}
>>
>> -(id)initWithContext:(ContextB*)context;
>>
>> @end
>>
>> The problem is that when I call
>>
>> ContextA* context = ...;
>> [[ClassA alloc] initWithContext:context];
>>
>> Not in all cases, but in some I get the warning that the var "context"
>> is of different type (ContextA) than expected (ContextB). It seems
>> like the compiler erroneously resolves this call to ClassB instead of
>> ClassA, I guess because the two methods have the same name. This
>> problem disappears if I rename one of the methods to
>> "initWithContext2".
>>
>> I'd like to keep the same name and I hate the compiler warning. Is
>> there any resolution? Is it really a bug of the Obj-C compiler?
>
> No, it's not a bug.
>
> The problem is that the compiler doesn't know which method you mean
> because the "alloc" call returns an id. Either change the names of
> your methods (which might make sense given that "context" doesn't tell
> you much) or cast the return from alloc to the appropriate type. You
> could use a macro to make it slightly more readable.
>
> -- Chris
>
_______________________________________________
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