[cfe-dev] Objc, cannot cast 'super' error
Dave Keck
davekeck at gmail.com
Sun Jul 3 04:15:26 PDT 2011
Hey list,
Clang doesn't allow super to be casted; I'd like to present my
use-case demonstrating why I think this should be allowed, or get some
feedback on why Clang is right and my design is wrong:
I have two classes, ClassA and ClassB. ClassB inherits from ClassA,
and ClassB alone implements MyProtocol:
@protocol MyProtocol
- (void)handleToken: (id)token;
@end
The contract for MyProtocol is that if you don't recognize 'token',
you send -handleToken: to super. If no superclass implements
-handleToken:, then a runtime exception is correctly generated since
no one could handle the token. (This is exactly the same as KVO's
-observeValueForKeyPath: contract.)
ClassB's implementation of -handleToken: triggers the "'ClassA' may
not respond to 'handleToken:'" warning on the call to super:
- (void)handleToken: (id)token
{
if (token == myToken)
... handle token ...
else
[super handleToken: token];
}
Without the ability to cast super, I have the following options, none
of which are optimal:
1. Add the following code to ClassB.m:
@interface ClassA (Stub) <MyProtocol>
@end
This option is bad because it'll need changing if ClassB's
superclass changes, and will have to be separated from the code that
triggers the warning.
2. Omit the [super handleToken: call], since (at present) no other
superclass actually implements -handleToken:.
This option is bad because it'll break if a superclass starts
implementing -handleToken:.
3. Make MyProtocol an informal protocol on NSObject instead.
This option is bad because a formal protocol is the correct
abstraction for my use-case.
4. Use objc_msgSendSuper directly.
This option is just ugly and inconsistent with the rest of my code.
Why is my design wrong?
Thanks!
David
More information about the cfe-dev
mailing list