4 ms·
> Override functions must use the `override` keyword. Now when you rename the superclass's function, all the subclass functions fail to compile because they're
by spicyj 12y ago
> Override functions must use the `override` keyword. Now when you rename the superclass's function, all the subclass functions fail to compile because they're not overriding anything. Why did no one think of this before?
As I understand it, Java has a similar `@Override` annotation:
http://stackoverflow.com/q/94361/49485 http://stackoverflow.com/q/94361/49485
- derekerdmann 12y agoC# also includes compiler checks to make sure you're explicitly overriding methods correctly: http://msdn.microsoft.com/en-us/library/ebca9ah3.aspx http://msdn.microsoft.com/en-us/library/ebca9ah3.aspx
- skwirl 12y agoC# takes it even further by not allowing you to override methods unless they were marked as overridable in the base class with the "virtual" keyword.
- deleted 12y ago[deleted]
- evmar 12y agoC++ also, though you need to wrap it in a macro to work on older compilers. https://code.google.com/p/chromium/codesearch#chromium/src/base/compiler_specific.h&l=136 https://code.google.com/p/chromium/codesearch#chromium/src/b...
- deleted 12y ago[deleted]
- pjmlp 12y agoEiffel, Object Pascal, Modula-3, Component Pascal and many others do as well. I guess the author is not well versed in programming languages history.
- fatihpense 12y agoI think he means, "Why did no one think of this before" in Obj-C
- ninkendo 12y agoJava's @Override is not only optional, but fails to address an important use case: when you don't want to override anything with your method, and then the base class has a method added to it with the same name as yours. (Or if you simply didn't know that the base class had that method.) Swift will make sure you don't inadvertently override base class functionality by requiring you to use it.
- cromwellian 12y agoIn Java, you'd just add a 'final' keyword to the base method and you'd get a compile time error if someone accidentally tried to override it. If you want the equivalent of !@Override, a) you can do it with Java8 annotation types and a checker or b) you can just declare the method private. I'm sure Swift's version also fails to address all of the edge cases people might want to catch. No type system can be complete and catch every use case.
- mercurial 12y agoYou can't always do b) since your object has to, well, do something. Personally, I'd like to make inheritance strictly opt-in (as in, classes marked final per default), if anything this may be enough to make some people think twice about the let's-use-inheritance-to-share-code antipattern.