Add an @override keyword on methods to help detect errors

Originator:ddribin
Number:rdar://7215146 Date Originated:10-Sep-2009 11:34 PM
Status:Open Resolved:
Product:Developer Tools Product Version:
Classification:Enhancement Reproducible:N/A
 
Overriding a superclass' method or implementing a protocol with optional methods can result in subtle bugs, including:

* Accidentally overriding a superclass' method.
* Misspelling a superclass' method, thus not actually overriding it.
* Misspelling an optional protocol method, thus not actually implementing it.

Java provides an '@override' annotation to handle these cases:

  <http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Override.html>

And C# provides an 'override' keyword:

  <http://msdn.microsoft.com/en-us/library/ebca9ah3.aspx>
  <http://msdn.microsoft.com/en-us/vcsharp/aa336813.aspx>

A similar keyword in Objective-C, like @override, could work the same way:

@override
-(void)drawRect:(NSRect)rect
{
}

If you mark a method with @override, it *must* be declared in either the superclass or an optional protocol method.  If not, you get a compiler error (or maybe just a warning).

Perhaps an @implement keyword could be used for protocols since you aren't strictly overriding anything:

@implement
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
{
}

This may be polluting keywords too much, and @override could be used for both cases, even if it's not strictly correct for protocols.

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!