godotengine / godot-proposals

Godot Improvement Proposals (GIPs)
MIT License
1.15k stars 97 forks source link

Add `nameof()` operator equivalent to GDScript #10117

Closed DevSavvySerge closed 3 months ago

DevSavvySerge commented 3 months ago

Describe the project you are working on

I'm developing a game where maintaining code robustness and readability is crucial, especially in dynamically accessing properties and handling event names.

Describe the problem or limitation you are having in your project

In GDScript, there is a lack of a nameof operator equivalent found in languages like C#. This operator allows developers to refer to the names of variables, methods, or properties as strings. Without it, developers resort to hard-coding string literals to access properties or to handle event names. This practice makes the code more fragile and prone to errors, especially during refactoring.

Describe the feature / enhancement and how it helps to overcome the problem or limitation

I propose introducing a nameof operator or equivalent feature in GDScript. This enhancement would enable developers to obtain the name of a variable, method, or property as a string at compile-time. By using nameof, developers can ensure that their code remains robust and maintainable when accessing properties or handling events, without relying on hard-coded strings.

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

Here's a basic outline of how the proposed enhancement could work:

Introduce a nameof function or operator in GDScript:


var property_name = nameof(some_object.some_property)

Use nameof to obtain the name of a method:


func some_method():
    var method_name = nameof(some_method)

Benefits of using nameof:

Enables safer and more readable code.
Reduces errors when referencing properties or methods.
Facilitates easier maintenance and refactoring.

If this enhancement will not be used often, can it be worked around with a few lines of script?

Currently, developers can work around this limitation by manually using string literals to refer to property names or method names. However, this approach is error-prone and can lead to code that is harder to maintain, especially in larger projects or during refactoring.

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

The nameof operator or equivalent feature is widely recognized as a best practice in programming languages like C#. Implementing it as a core feature in GDScript would promote code robustness, maintainability, and readability across projects within the Godot engine environment. It would also align with industry standards and developer expectations for modern development tools.

Gnumaru commented 3 months ago

https://github.com/godotengine/godot-proposals/issues/837

Calinou commented 3 months ago

Thanks for the proposal! Consolidating in https://github.com/godotengine/godot-proposals/issues/837.

nargleflex commented 3 months ago

Oh sweet, I was just looking for something like this!