Skip to content

Implement method overloading in GDScript #1571

Open
@me2beats

Description

@me2beats

Describe the project you are working on:
gdscript plugins

Describe the problem or limitation you are having in your project:
I often need to create similar methods that do similar things (and even usually return the same type), but differ in arguments (their number or their types).

The problem is that the more subtle the difference between the two methods, the more difficult it is to come up with a clear and concise names for the methods.
In addition, it is highly likely that in this case the methods will contain unnecessary redundant information that could easily be obtained from the methods signatures.

On the other hand, creating a single method using optional parameters partially solves the problem, but this creates difficulties in further extending the method, forcing to create a confusing code with a large number of if statements, and ultimately degrade the readability and support of the method.

Describe the feature / enhancement and how it helps to overcome the problem or limitation:
Implementing method overloading in gdscript would solve this problem in most cases

Describe how your proposal will work, with code, pseudocode, mockups, and/or diagrams:
Perhaps one of the most beautiful illustrations of how convenient method overloading is, is the Color() methods.
Color

Note how method overloading saved the engine developers from having to create methods like
hex2color()
int2color()
etc

so in gdscript, one could declare several methods with the same name but different parameters.
when trying to declare 2 methods with exactly the same signatures, an error would appear:

The function "x" with signature ... already exist in the class

and when calling such an overloaded method, one just needs to choose a suitable signature, that could pop up just like in the case of Color():
Color2

If this enhancement will not be used often, can it be worked around with a few lines of script?:
above I indicated 2 workarounds (one is to set appropriate method names and the other is to use "universal" methods - all in one - with optional parameters), this may be acceptable in small projects, but most likely will lead to difficulties in maintaining such code as the project grows

Is there a reason why this should be core and not an add-on in the asset library?:
I think this should be core feature as it's directly related to gdscript

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    • Status

      On Hold

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions