Override Keyword?

Monkey Forums/Monkey Programming/Override Keyword?

Tibit(Posted 2011) [#1]
I can use Abstract so that a method is Required to be overloaded, however I find myself many times wanting an Override command in addition to that.

Let us say I have a Component:

Class Component
Method OnRender() Abstract
Method OnUpdate() Abstract
End

In this case if I want to Extend this component I have to also Implement OnRender and OnUpdate.

However I might want OnRender to be optional:
Class Component
Method OnRender()
End
Method OnUpdate() Abstract
End

So I do above. My components are now requried to implement OnUpdate, but OnRender is optional - if not implemented the default found in the Base Component will be used.

This is also fine.

However would I change OnRender to Render for example. Then I might think (and anyone else using that library code) that they are doing fine. However in reality they are now attempting to Override a method that does not exist.

So I suggest the Override Keyword

Class MyComponent Extends Component
Method OnRendor:Void() Override

End
End

Above would say: "Could not find an 'OnRendor:Void()' method to Override in any base class"

I suggest the Keyword is optional so nothing would change. It would make Refactoring a lot nicer since changing a Framework Method would actually (assuming the use of Override) pop an compile error.

A simple addition, useful for those using building and implementing libraries and frameworks.

What do you think?


Samah(Posted 2011) [#2]
Mandatory in .NET and other languages, and a compile-time annotation for Java 5+ (optional, but can be made mandatory via compiler flags).

I'm all for this.

BTW: Your Component class should be Abstract too. The fact that it works without it is a bug in trans (hint, hint Mark, plz fix). :)

On a somewhat related note (not to hijack your thread), I'd love the ability to put the Const keyword on classes (to prevent extending) and methods (to prevent overriding).