Reserved keywords in Objective-C?

Felixyz picture Felixyz · Dec 9, 2009 · Viewed 10.9k times · Source

At the CocoaHeads Öresund meeting yesterday, peylow had constructed a great ObjC quiz. The competition was intense and three people were left with the same score when the final question was to be evaluated: How many reserved keywords does Objective-C add to C?

Some spirited debate followed. All agreed that @interface, @implementation etc are all pre-processor directives rather than keywords, but how about something like in? It might be a keyword, but it's not a reserved keyword. For example, the following will compile without errors or warnings:

NSArray* in;
for (in in in)
   NSLog(@"bwahahaa");

We concluded that ObjC adds no reserved keywords to C, and someone won a seemingly well-earned book.

But today I tried some more systematic abuse on the compiler by trying things like this:

int self = 45;
self++;
int y = self;

That compiles fine, and the same code works replacing self for BOOL, bycopy, inout, oneway, byref, SEL, and IMP.

Using id as the variable name, the first and last lines compile, but not the second one. The same goes for Protocol, and Class.

Using super, the first line compiles, but not the second and third.

With YES, NO, and NULL, all three lines fail to compile, probably because they are just defined as true, false, and nil.

It looks to me like a lot of this is gcc getting confused and I'm not so sure it reflects what is and isn't a reserved keyword in Objective-C. Why, for example, is it ok to use self as the name of an int, but not super?

The fact that the first assignment always works (except for with YES, NO, and NULL) would seem to support the idea that none of the candidates are technically reserved keywords that are not found in C. Or?

Could someone please give us an authoritative explication of this thorny issue?

Several people's honor is at stake.

EDIT: As Nikolai Ruhe pointed out, we need a clear definition of "keyword" to proceed. Niko cited a Wikipedia article saying that a keyword is "a word or identifier that has a particular meaning".

I think it is reasonable to use this definition from the same article:

In many languages, such as C and similar environments like C++, a keyword is a reserved word which identifies a syntactic form. Words used in control flow constructs, such as if, then, and else are keywords. In these languages, keywords cannot also be used as the names of variables or functions.

Furthermore, as the article states:

Typically, when a programmer attempts to use a keyword for a variable or function name, a compilation error will be triggered.

In this sense, then, are there any reserved keywords that are predefined in the language’s formal specifications and cannot be used as a user-defined name?

Answer

NSResponder picture NSResponder · Dec 9, 2009

All agreed that @interface, @implementation etc are all pre-processor directives rather than keywords

Then all were mistaken. #import and #pragma are preprocessor directives. @interface, @implementation, @protocol and so forth are keywords of Objective-C, and they are compiler directives. They haven't been preprocessor directives since NeXT extended GCC to compile Objective-C without a Stepstone's original Objective-C pre-processor implementation.