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: Patryk Laurent
Subject: Re: Why/how is count method not emitting warning/error on id?
Date: Sat, 08 Jul 2017 10:27:22 -0700


On Jul 8, 2017, at 09:46, Ivan Vučica <ivan@vucica.net> wrote:

"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 :)

Thanks Ivan, that explains it!  Mystery solved :)

Very helpful behavior for using id in a codebase that the compiler sees in its entirety.

Patryk










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]