-
-
Notifications
You must be signed in to change notification settings - Fork 98
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add "alias" keyword for functions in GDscript #9768
Comments
Aliases tend to be frowned upon in APIs, they give users several ways to achieve the same thing. This leads to inconsistent across different users of the API, along with choice paralysis. Also, I don't know of any programming language which has syntactic sugar for this (other than through preprocessor defines). |
Ruby has class SomeExistingClass
alias_method :some_existing_method, :new_name
def some_existing_method
new_name # Call original implementation.
# extra code goes here
end
end Whenever something uses the patched method in this class, it will call the original method plus some extra code. I don't see any use for it in GDScript though, as it doesn't support monkey patching. Also aliases wouldn't solve the same problem as e.g. |
@Calinou you're right on the use cases, this would only make sense for API or (extreme case) a project with multiple people that have different preference on the function name. @KoBeWi This could be used by the projects in the asset library, since some of the implements API and libraries in GDScript. Adding a new method as the alias for monkey patching would be more useful, an actual alias keyword would only have a disadvantage and make it look faulty. Thinking about it now this feature alone would be too small as an addition because of the (extreme) rare use case, I've used in some of my projects aliases but because I'm working with other people that asked me to add them. I got the point and I haven't thought of it before, GDscript is too simple. Even if someone else would need something like this feature they are probably making something such as an API or library that may need this complexity, and it that case they would probably use C# or C++ and bind it to GDScript. |
Why not just use a wrapper? func base_func(a, b, c = 0):
return a + b + c
func alias_func(a, b, c = 0):
return base_func(a, b, c) Disadvantages:
However, this only matters if you create a lot of aliases, which is not generally considered good practice. |
|
Describe the project you are working on
I want share an idea to add an "alias" keyword for gdscript that allow to give a function multiple names to call it.
Similar to what
Array.append
is just an alias ofArray.push_back
, it can be defined in a script inside a class by the user.Describe the problem or limitation you are having in your project
It could be used as syntax sugar.
Describe the feature / enhancement and how it helps to overcome the problem or limitation
In GDscript this keyword could be added either as an actual keyword:
Or as a decorator but it doesn't look good:
Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams
Here how it could look like on a real use case:
When calling the function "append" it will instead call the function "add", the inspector may show a message that an alias has been called showing something like this in the frame stack:
An alias would ask the same amount of parameters of the original function:
The engine will jump from the alias to the function it refeers to. In debug mode this could be done each time (to show it in the messages), and in release aliases are removed from scripts and replaced with the original function.
It could show an error if the alias is already used like "alias name already in use" and shouldn't be used on lambdas.
The autodocs could display the alias as a function and add the message "alias of ()".
If this enhancement will not be used often, can it be worked around with a few lines of script?
In current GD script you would just define a new function that calls the original one, it will uses one extra stack call. If the function has argouments you would have to copy all of them as well.
Is there a reason why this should be core and not an add-on in the asset library?
.
The text was updated successfully, but these errors were encountered: