Re: Methods with the same name but different return types
Re: Methods with the same name but different return types
- Subject: Re: Methods with the same name but different return types
- From: Eyal Redler <email@hidden>
- Date: Fri, 03 Jun 2011 16:23:48 +0300
> There is a warning flag in Xcode "Strict Selector Matching" ( [GCC_WARN_STRICT_SELECTOR_MATCH, -Wstrict-selector-match] in clang, gcc) which is disabled by default. If enabled, the compiler warns if it finds multiple methods with different arguments or return types for a given selector - but only if the receiver is "id" or "Class".
>
> You can enable this flag, but you won't get a warning if you had written your statement as follows:
>
> float myValue = [(MyClass*)[myArray objectAtIndex:i] position]+kMyConstant;
>
> Since you explicitly cast from "id" to MyClass* the compiler omits the warning. In this case, the compiler assumes you know exactly what you are doing.
>
>
>
> If you had written it as below
>
> float myValue = [[myArray objectAtIndex:i] position]+kMyConstant;
>
> and the flag -Wstrict-selector-match is set, the compiler will warn you - since there is an ambiguity.
>
> In praxis, you would now need to resolve this ambiguity, through explicitly cast to the return type you expect (as you did).
That looks very close to what I need. Unfortunately it gives too many false warnings about cases where the types are not ambiguous. I'll try turning it on temporarily, scan for real potential problems and turn it off...
>> 2. What possible side effects could such a mismatch lead to? Is it really possible that this could lead to round() returning NaN (sometimes)?
>
> I would say, if there are more than one possibly return types for the same selector, the code is illegal unless you explicitly type cast to the correct return type. Of course, assuming it is actually the type you expect. Otherwise, it is a programmer error.
>
> If you erroneously cast to a wrong type, or if there is an ambiguity the side effects will be random. I guess, its very possibly that you see this effect.
I think my original post wasn't clear enough. I saw the side effects (like round() retuning NaN) when I _wasn't_ casting. Once I've cast, the problems disappear. I just can't be 100% sure that the problems are directly related because the side effects were never consistent and I also don't really understand how these effects could take place considering the fact that both integer and float (on my 32bit application) are the same size.
So, I understand that this statement:
float myValue = [[myArray objectAtIndex:i] position]+kMyConstant;
Gives me a bad value for "myValue" if the compiler treats "position" as a method returning integer. But is it possible that this would also cause something like round() retuning NaN for a perfectly normal number? (at least as far as I could see in the debugger)
Eyal
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden