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?
Re: Nikolai Ruhe's suggest that self is a keyword
self
is a parameter to an Objective-C method of typeid
(similar to how_cmd
is also a param, of typeSEL
) and not a keyword.nil
is a macro which expands toNULL
. I'm a little disappointed thatsuper
doesn't expand to a macro, nor is it a parameter.From the wikipedia entry:
That would preclude Objective-C from adding any restricted keywords to the language.
A look at the Objective-C Programming Language gives you some clues about what is possible or not. As some "keywords" are defined in a header file, their substitution can lead to observed (and unexpected) behavior.
Wikipedia defines keywords as:
I'd translate that to: a keyword in a language is a non-terminal in its grammar. (Btw, the grammar doesn't contain the word "keyword" just by itself, so no confusion there).
The terminals in the grammar would be:
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.Grammar in Appendix B of the original 1995 book defines
super
as "literal symbol" terminal and it's a special-case in message receiver syntax.in
literal symbol is part of protocol-qualifier in ObjC 1.0 and was reused in fast enumeration syntax in ObjC 2.0 (which has no formal grammar definition AFAIK).id
is defined as part of type-specifier alongsidevoid
,char
,unsigned
, typedefed types, etc. If you callvoid
a keyword, thenid
is too.You could check out The Objective-C Programming Language.
As far as I can tell, that document doesn't classify anything as a "reserved word". The
@implementation
,@interface
, etc. are called "compiler directives."