godotengine / godot-proposals

Godot Improvement Proposals (GIPs)
MIT License
1.17k stars 98 forks source link

Automatically infer return types in GDScript #11177

Open btarg opened 1 week ago

btarg commented 1 week ago

Describe the project you are working on

An RPG entirely written in GDScript, with a preference for only using statically-typed variables and functions

Describe the problem or limitation you are having in your project

When you write a function in GDScript which does not return anything, you can write it two ways:

func hello_world():
    print("hello world")
func hello_world() -> void:
    print("hello world")

Both are functionally identical, but the second option should provide better performance, as it is statically typed.

Functions written like the first example may also present a pitfall for newcomers to a codebase, as a developer will be unsure of what the function returns without opening the function definition. In a large-scale collaborative project, this could lead to an issue such as a developer trying to assign the function's non-existent return value to a variable.

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

Godot should be able to automatically detect that this function has no return type and make the snippet in the first example internally act the same as the second example. In theory, this should yield better performance with little risk, as we have already determined that the function does not contain a single return anywhere.

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

Hovering over a the first example function in the script editor would show the programmer that the function returns void.

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

This does not necessarily have a "work-around," other than encouraging the writing of statically-typed code.

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

This is part of GDScript

Calinou commented 1 week ago

This was proposed in the past and was rejected: https://github.com/godotengine/godot-proposals/issues/8371

dalexeev commented 6 days ago

Both are functionally identical, but the second option should provide better performance

As for performance, the compiler already checks whether the function body has a return, no matter whether there is an explicit void or not. Also, the documentation generator detects implicit void functions too. (But we don't have a distinction between empty and non-empty return at the moment, so it might not work correctly.)

However, explicit and implicit void functions have a difference for the static analyzer. Unlike the compiler, it assumes that a function with an untyped return value may return something, even if in fact it doesn't. In theory, we could change the latter, but there would still be a fundamental difference between explicit and implicit void functions. By specifying void explicitly, you protect yourself from accidental errors, while with implicit void GDScript cannot determine whether this was intentional or accidental. See the issues linked above for details.