tags:

views:

294

answers:

8

In Objective-C, you can invoke class methods with:

[MyClass aClassMethod];

And you can query an instance's kind with:

[someInstance isKindOfClass:[MyClass class]];

But, why do we need to do [MyClass class], and not simply provide MyClass like this:

[someInstance isKindOfClass:MyClass];

Is there a reason that the compiler is fine with encountering MyClass as a receiver (a pointer type) but not as an argument? Is it a limitation of parsing the language? Or perhaps a limitation of the compiler?

A: 

Because what the isKindOfClass expects is a "class" and that's what get returned from invoking: [MyClass class]

OscarRyz
But why is `MyClass` by itself not a class, if it is already a valid receiver?
dreamlax
because `MyClass` is-a `NSObject` ( most likely ) since it inherits `NSObject` and not a `Class` see: http://developer.apple.com/mac/library/documentation/cocoa/reference/foundation/Protocols/NSObject_Protocol/Reference/NSObject.html#//apple_ref/occ/intfm/NSObject/isKindOfClass:
OscarRyz
Then, howcome `[[MyClass class] someMethod]` and `[MyClass someMethod]` each invoke the same method?
dreamlax
+1  A: 

My first glance answer is because [MyClass class] return a object of type Class, and MyClass doesn't inherit from Class...

John Biesnecker
If this were the case, then it would be a type mismatch error, but it isn't. The compiler can't seem to treat `MyClass` as an expression.
dreamlax
Consider: `id f1 = [NSFileManager defaultManager]; id f2 = [[NSFileManager class] defaultManager];` This would seem to indicate that `[NSFileManager class] == NSFileManager` (and yes, `f1 == f2`)
Dave DeLong
John, `Class` is a [data-type](http://developer.apple.com/mac/library/documentation/cocoa/reference/objcruntimeref/Reference/reference.html#//apple_ref/doc/uid/TP40001418-CH3g-objc_class) used by the Objective-C runtime, not an Objective-C class.
Georg Fritzsche
WRONG -- MyClass is a class, it just cant be used anywhere other than as the receiver of a message. @Georg -- Class is both the data type and an Objective-C class -- it is how it is represented internally to the runtime.
Jared P
@Jared: There is a difference between `objc_object*` et al and `Class`.
Georg Fritzsche
@Georg yes and no. Yes that they're declared differently, but no that you can use a Class anywhere you can use an `id`. As long as the first member of the struct is `Class isa`, you can treat is as an object.
Dave DeLong
A: 

MyClass is not of type Class.

[MyClass class] is of type Class.

If you are familiar with Java, the concept is the same.

java.lang.String is not of type java.lang.Class

java.lang.String.getClass() is of type java.lang.Class

ryanprayogo
"MyClass is not of type Class" => Then why can you do `[MyClass myClassMethod]` and also `[[MyClass class] myClassMethod]` and get the same functionality?
Dave DeLong
+6  A: 

Interesting.

In Objective-C, class name has two roles, as a data type and as a class object. As a data type name, you can do things like:

MyClass *anObject;

As a class object, the class name can stand for the class object only as a message receiver. And this is why you have to use

... isKindOfClass:[MyClass class] ...

However, I don't think this is the answer which can satisfy your need. To me, the answer is, "yes, what you want is plausible. But the spec says the other way".

Reference: The Objective-C Programming Language Page 32, section: "Class Names in Source Code".

yehnan
This is basically a C compatibility thing. The receiver position is special-cased to allow class names, but everywhere else it's treated as a normal C type.
Chuck
And @Chuck gets the prize! You should submit that as the answer, Chuck. That's the real reason.
Matt B.
A: 

I'm thinking that MyClass is actually a meta-class. You send it the class message to get the actual class (of type Class).

Steve Weller
+3  A: 

@John and @ryanprayogo -- you are both fundamentally wrong. MyClass is a Class, which is also an object, but does not inherit from NSObject. Objective-C is kind of weird this way, but its actually brilliant when fully explained (See here). The answer here, though, is as @yehnan said, that a class name can be either a type name for declarators and casts, or as a receiver for messages. The implementation of [MyClass class] returns self (which is, within the method, MyClass). Also as @yehnan said, the language could support passing it as an argument, although it simply doesn't.

Jared P
+1  A: 

@yehnan captures it well, but I'll expand on this a little. Yes, the compiler could be modified to automatically convert a class identifier into its applicable Class in places where it is an argument rather than only when it is the target of a message. But there's not a lot of call for that kind of added complexity in the compiler (translated: slower, harder to detect coding errors). You shouldn't be calling things that return Class very often. If you are, then your object model is broken. Class-checking should be the last, desperate approach after everything else has failed (most notably correct typing and then respondsToSelector:). So for this kind of rare event, it doesn't make a lot of sense to complicate the compiler this way.

Rob Napier
What if you were implementing a runtime?
dreamlax
What kind of runtime? If it's built on ObjC's runtime, then you'd mostly work in the C calls like class_getClassMethod() anyway. And a runtime still would mostly be interested in Class variables, not parsing hard-coded class identifiers. What are you building that you're needing to hardcode class identifiers often?
Rob Napier
I was talking about making an alternative Objective-C runtime, but I'm not building anything, I'm just curious to know how the parser interprets `MyClass` in various contexts.
dreamlax
The runtime isn't really involved here. The runtime is the C code that the compiler converts the ObjC into (in theory; in practice of course it bypasses the actual C output, but it compiles it basically as though your code were written in C. Look at the assembler output and you'll see all the calls to objc_msg_send()). There's no big difference between class and instance calls. Class calls just pull their pointers from the list of class references (i.e. L_OBJC_CLASSLIST_REFERENCES_$_0). But it's just the first parameter to objc_msg_send().
Rob Napier
+19  A: 

Ooooh... fun question. The answer is a c-ism.

Consider:

@interface MyClass : NSObject
@end
@implementation MyClass
@end

Now, say you have:

...
MyClass *m = nil;
...

In that context, the compiler sees MyClass as a type definition. The * says that the variable m is a pointer to a hunk o' memory that contains one (or many -- don't forget your C pointer-fu) MyClass instances.

In other words, MyClass is a type.

But, in the context of something like:

[someInstance isKindOfClass: x ];

x must be an rvalue or, in human terms, the value of an expression. A type, however, cannot be used as an rvalue.

That [MyClass class] works is actually a bit of a hack, both in the language and the compiler in that the grammar specifically allows a type name to be the message receiver (to be the target of a method call).

And, as a matter of fact, you can do:

typedef MyClass Foo;
....
[MyClass class];
[Foo Class];

It'll all work. However, you can't do the following but the error message is illuminating:

[NSUInteger class];

error: ‘NSUInteger’ is not an Objective-C class name or alias


Now, why not special case it everywhere as a bare name?

That colludes type names and rvalues and you quickly end up having to swallow something like [foo isKindOfClass: (MyClass)]; while barfing on [foo isKindOfClass: (MyClass *)]; which then encroaches upon typecasting territory in a rather uncomfortable fashion.

bbum
I was just about post a comment on my question saying "I wonder if `bbum` has an answer..."
dreamlax
@dreamlax @bbum *always* has an answer. He's just that awesome. :)
Dave DeLong
Oops, `s/wonder if/know that/` and `s/an/the`
dreamlax
Nah-- I just know where to look to refine my generally well educated guess. This one required a bit of digging in the C spec and the Obj-C sources. Fun question.
bbum
@bbum Yeah, but you captured the heart of the problem really well, and your insight in the last paragraph is particularly enlightening. I already knew the answer, and still I learned another way to think about it and a good example of the compiler problem. Thanks.
Rob Napier
Would using something like `isKindOfClass:@class(MyClass)` (if it could be used like this) allow the compiler to properly parse the type name, and avoid the dispatch at runtime? (similar to `@protocol`).
dreamlax
@dreamlax The compiler could be taught to consume something like that. It would have a subtly different meaning in that `[MyClass class]` actually does cause a method invocation that might return something different. @Rob I'm just the conduit for many brilliant minds...
bbum