Nice! Glad it helps.

You should, of course, still cast into a known type (and maybe store in a
temporary variable) any id-type expression, if you can. Possibly check it
with -isKindOfClass: or -respondsToSelector: if you are not sure what's in
the id-typed expression.

On Sat, Jul 8, 2017, 18:27 Patryk Laurent <[email protected]> wrote:

>
> On Jul 8, 2017, at 09:46, Ivan Vučica <[email protected]> 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 <[email protected]>
> 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.
>
>
_______________________________________________
Discuss-gnustep mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/discuss-gnustep

Reply via email to