Skip navigation.
 
mlRe: [Obj-C Compiler Bug?] Two different classes declaring a message with the same name - compiler warning
FROM : Roland King
DATE : Tue Oct 14 11:47:26 2008

Oleg Krupnov wrote:

>You are right, casting from alloc worked, thank you.
>
>Is it the recommended practice to always cast after alloc?
>

>

Not really - the recommended practice is to have method names which have
unique parameter types. Remember that Objective-C's notion of a selector
is foo:bar:baz:, it doesn't encode what the types of the parameters are
(or the return type). So if you  make every foo:bar:baz: have the same
parameter types even though the compiler isn't going to enforce that for
you, life gets simpler. If you have something which is very generic,
like setDelegate: then you could make the parameter an id and test it in
the code.

I have found myself more and more doing methods like this

-(void) methodWithSomeClass:(SomeClass*)someClassArg
anotherClass:(AnotherClass*)anotherClassArg
aSimilarClass:(ASimilarClass*)aSimilarClassArg;

and using the class names in the selector. It's verbose but ..
objective-C seems to be verbose (I wish I could get XCode to fill in a
template for the stuff I defined in the .h file when I go to define it
in the .m file though)

>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?

>

Well I'd expect the compiler remembers the methods available for each
class, cached under the selector name (remember no argument types in the
selector name). So if you have two identically named selectors in two
different classes but you call them ON instances of that class, all is
fine. For id, the most generic class, I'd expect it caches the first one
it finds with that selector name, then if you use it with different
parameters, it warns you then. It could warn you at the point you
declare the selector with different arguments, but that would be
unecessary, because provided you used it on class instances, not ids,
there's no ambiguity. So warning you at the point you use it on an id
makes most sense.

Related mailsAuthorDate
ml[Obj-C Compiler Bug?] Two different classes declaring a message with the same name - compiler warning Oleg Krupnov Oct 14, 10:43
mlRe: [Obj-C Compiler Bug?] Two different classes declaring a message with the same name - compiler warning Chris Suter Oct 14, 11:00
mlRe: [Obj-C Compiler Bug?] Two different classes declaring a message with the same name - compiler warning Oleg Krupnov Oct 14, 11:17
mlRe: [Obj-C Compiler Bug?] Two different classes declaring a message with the same name - compiler warning Roland King Oct 14, 11:47
mlRe: [Obj-C Compiler Bug?] Two different classes declaring a message with the same name - compiler warning Graham Cox Oct 14, 11:52
mlRe: [Obj-C Compiler Bug?] Two different classes declaring a message with the same name - compiler warning Chris Suter Oct 14, 11:58
mlRe: [Obj-C Compiler Bug?] Two different classes declaring a message with the same name - compiler warning Graham Cox Oct 14, 12:13
mlRe: [Obj-C Compiler Bug?] Two different classes declaring a message with the same name - compiler warning Chris Suter Oct 14, 13:11
mlRe: [Obj-C Compiler Bug?] Two different classes declaring a message with the same name - compiler warning Graham Cox Oct 14, 13:31
mlRe: [Obj-C Compiler Bug?] Two different classes declaring a message with the same name - compiler warning Quincey Morris Oct 14, 18:59
mlRe: [Obj-C Compiler Bug?] Two different classes declaring a message with the same name - compiler warning John Engelhart Oct 15, 12:03
mlRe: [Obj-C Compiler Bug?] Two different classes declaring a message with the same name - compiler warning Chris Suter Oct 15, 12:36
mlRe: [Obj-C Compiler Bug?] Two different classes declaring a message with the same name - compiler warning Jonathan del Strot… Oct 15, 12:50
mlRe: [Obj-C Compiler Bug?] Two different classes declaring a message with the same name - compiler warning Michael Ash Oct 15, 16:17