godotengine / godot-proposals

Godot Improvement Proposals (GIPs)
MIT License
1.12k stars 69 forks source link

Add a `const`/`final` qualifier for methods that shouldn't be overridden #1311

Open jonbonazza opened 4 years ago

jonbonazza commented 4 years ago

Describe the project you are working on: MMO framework

Describe the problem or limitation you are having in your project: I have some classes that I want users to be able to extend, but there's certain methods of those classes that I don't want them to be able to override, as they are integral to the function of the overall framework.

Describe the feature / enhancement and how it helps to overcome the problem or limitation: In many languages, functions/methods can be marked as const or final which tells the compiler/interpreter that it cannot be overridden. I suggest the same design for GDScript. Since we already have const for constants, we might as well just use that here too.

Describe how your proposal will work, with code, pseudocode, mockups, and/or diagrams:

# the function is marked as const, so subclasses cannot override it.
const func foo(bar : String) -> void:
    print(bar)

If this enhancement will not be used often, can it be worked around with a few lines of script?: It cannot currently be worked around easily.

Is there a reason why this should be core and not an add-on in the asset library?: GDScript is a core component.

Calinou commented 4 years ago

Marking a method const might be confusing, as people might think it disallows those methods from modifying member variables and calling non-const methods (like in C++) or means the method is run at compile-time (like Rust).

What about a @final annotation? We've already settled on using annotations for auxiliary things that change the script's behavior after all.

jonbonazza commented 4 years ago

I dont particularly care what the syntax is. I think that a keyword makes more sense than an annotation here since it's a keyword in every other language that supports it and would be familiar to devs, but really dont feel strongly about it at all.