discuss-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Why/how is count method not emitting warning/error on id?


From: Ivan Vučica
Subject: Re: Why/how is count method not emitting warning/error on id?
Date: Sat, 08 Jul 2017 17:46:15 +0100
User-agent: K-9 Mail for Android

"id" is in general the "don't ask me, I know what I'm doing" type.

So in theory, you could be getting no warning at all.

That you get a warning at all is clang playing smart and saying "absolutely nowhere, in any of the types that I saw in this compilation unit, did I see this method".

If you called any method known in any class (e.g. in any class defined in a header you included) you'd also not get a warning.

If you called a method that is defined in another file without including the header, you'd get the same compile time warning even though the code would work.

Let me know if this is unclear and I'll try to rephrase – I know this is not the clearest way to explain it :)

On July 8, 2017 4:04:29 PM GMT+01:00, Patryk Laurent <plaurent@me.com> wrote:
Hello,

While experimenting with distributed objects, I noticed that the count method can be called on an id but I cannot just call any arbitrary method on id (e.g., a made-up "countx" method).
Why doesn't calling count on an id generate a compiler warning/error-when-using-ARC?  
It seems the count method is accorded some special status compared to an arbitrary method like "countx"... how does this work?

Code example including the somehow-working [id count] (but erroring other cases, as expected) are shown below.

Thank you,
Patryk


#import <Foundation/Foundation.h>

int main() {
    id _one_ = [[NSObject alloc] init];
    NSObject* two = [[NSObject alloc] init];
    [one count];
    [one countx];
    [two count];
    [two countx];
    return 0;
}

When compiled with ARC:


blessed_methods.m:9:10: warning: instance method '-countx' not found (return type defaults to 'id'); did you mean
'-count'? [-Wobjc-method-access]
[one countx];
^~~~~~
count
blessed_methods.m:10:10: warning: 'NSObject' may not respond to 'count'
[two count];
~~~ ^
blessed_methods.m:11:10: warning: instance method '-countx' not found (return type defaults to 'id')
[-Wobjc-method-access]
[two countx];
^~~~~~




--
Sent from my Android device with K-9 Mail. Please excuse my brevity.
reply via email to

[Prev in Thread] Current Thread [Next in Thread]