Re: New Cocoa Programmer
Re: New Cocoa Programmer
- Subject: Re: New Cocoa Programmer
- From: Ondra Cada <email@hidden>
- Date: Tue, 13 Nov 2001 10:41:08 +0100
Louis,
>
>>>>> Louis Demers (LD) wrote at Mon, 12 Nov 2001 18:55:06 -0500:
LD> >>If it is *not* the case, might you perhaps know why the following code
LD> >>is uncompilable?
LD> >>
LD> >>... switch (x) { case @selector(xyz): ... }
LD> >>
LD> >>That should be (from the compiler's prespective) a pretty equivalent
LD> >>situation as using class names there, or do I understand it improperly?
LD> >
LD> >I would hazard a guess here; for the same reason that
LD> >char* string;
LD> >switch (string) {
LD> >case "abc":
LD> >...
LD> >case "xyz":
LD> >...
LD> >}
LD> >won't work. Aren't selectors and such ultimately implemented as
LD> >(possibly mangled) strings?
LD>
LD> unless OBJ C has seriously extended std. C, your switch statement can
LD> only use ints.
That's a bit more complicated. Of course switch takes ints and compares the
value in the switch statement as int to the case: values.
*BUT*, you have to consider the fact that in C pretty much anything can be
easily converted to an int! Between many value types which are freely
int-conversible are both selectors and pointers (ie., strings, because C
strings are just pointers, type const char*).
Therefore, from the compiler's perspective it *should* work both: selectors
and strings (and classes, presumed you can use their names directly as
values, as it was possible in some quite ancient gccs of NeXTStep 2.x or so).
Though: you _can_ use all these values in, say, ifs, store them to int
variables, print them using the "%d" or "%x", but you _CAN'T_ use them in
case: labels. That means they are ints all right, but they are not constant!
That's the tricky thing: selectors or class addresses or C strings _ARE_
constants, as a matter of fact. I am *not sure here*, but I guess the reason
the compiler "does not know that" is that they are not compile-time
constants, but link-time ones. Therefore, I can quite well imagine that the
compiler can't construct some fixed jump table for the switch statement,
since it does not know the constant values yet -- they will be filled in by
linker... That was what I asked for -- to disprove or support this guess.
LD> take the string pointer and compare it with the pointer to "abc",
LD> "xyz" .... That would meaningless since having another string "abc"
LD> would be equivalent but using another memory location, it would be
LD> judged different.
Yes and no -- I do agree it's *quite* meaningless, but not all that much: a
number of compilers coalesce equivalent string constants. Gcc does, too:
5 tmp/smazat/tmp> cat q.m
#import <stdio.h>
int main (int argc, const char * argv[]) {
const char *string="aaaa";
printf("%x %x %x\n",string,"aaaa","aaaa");
return 0;
}
6 tmp/smazat/tmp> cc q.m && ./a.out
1f5c 1f5c 1f5c
7 tmp/smazat/tmp>
---
Ondra Cada
OCSoftware: email@hidden
http://www.ocs.cz
2K Development: email@hidden
http://www.2kdevelopment.cz
private email@hidden
http://www.ocs.cz/oc