[cfe-dev] Introducing attribute objc_pure_virtual_method

Louis Gerbarg lgerbarg at gmail.com
Tue Oct 29 21:55:02 PDT 2013


On Tue, Oct 29, 2013 at 7:32 PM, Ted Kremenek <kremenek at apple.com> wrote:

> On Oct 29, 2013, at 3:16 PM, variadictemplate . <
> variadic.template at googlemail.com> wrote:
>
> currently there is no possibility in objective-c to mark a method as "pure
> virtual" like in C++. I think the "pure-virtual"-construct enables an
> elegant and clean way to describe and specify APIs, although if it may be a
> rarely used construct.
> A workaround for objective-c is to declare the method within the
> base-class and add to it's implementation an assertion like assert(false &&
> "has to be implemented by subclasses"). But i think it would be nicer when
> the compiler gives me a hint that there is something wrong, instead of
> crashing the app at runtime.
>
> So, with my attached patch I'm introducing an attribute for that case,
> named objc_pure_virtual_method. It can only be attached to methods of a
> class-declaration (the other way round: extensions, categories and
> protocols are excluded). If a subclass does not implement the method, it
> will cause a warning. If the subclass redeclares the method within it's
> @interface-declaration and also adds the objc_pure_virtual_method, no
> warning will be generated. If the call of an init-method to an instance of
> a class with pure virtual methods is detected, an error is generated.
>
> I'm not sure if the init-call to a subclass of an abstract class, that did
> not implement all pure virtual methods, should also produce an error, as
> this case already produces warnings of unimplemented methods - in my eyes,
> this is sufficient.
>
> Surely there are some issues with my current implementation, as i'm not so
> sure if i have chosen the right places to add the logic and about the names
> / warnings i have chosen and I'm happy to hear any suggestion / criticism<http://www.dict.cc/englisch-deutsch/criticism.html>
> .
>
>
>
I have also been in a situation where I have done the assert(false && "has
to be implemented by subclasses") thing, but it is very rare. In Objective
C subclassing is (compared to most OOP languages) infrequent and shallow.
Since most the framework code in Cocoa/CocoaTouch/GNUStep chooses to use
delegation as opposed subclassing it is generally possible to achieve the
same sort of compile time warnings by having @required methods in a formal
protocol on a delegate.

While I am generally in favor of adding annotations, I wonder if it is
worthwhile to add an annotation for something that is really not a common
idiomatic pattern in the language?

(2) In a subclass, if a method is redeclared as being “non-virtual”, should
> we also guarantee that it is implemented in the @implementation?  That may
> be tricky of course with categories.
>

I suspect you can get into similar situations today where the compiler does
not have enough info to do things with @required in formal protocols and
categories. I am sure you can if you are using class_addMethod to provide
the IMPs at runtime. The language already provides @dynamic for dealing
with that with respect to properties. Extending @dynamic to methods would
work for the proposed annotation (if it makes sense to do the annotation at
all) and solve the issue with formal protocols (though in practice I am not
sure I have seen the cases ever come up, they are sort of pathological).

Louis
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20131030/8ba7cf83/attachment.html>


More information about the cfe-dev mailing list