Skip to content
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

Statically typed Callable #10807

Open
sockeye-d opened this issue Sep 22, 2024 · 3 comments
Open

Statically typed Callable #10807

sockeye-d opened this issue Sep 22, 2024 · 3 comments

Comments

@sockeye-d
Copy link

Describe the project you are working on

Any project that uses lambda functions heavily

Describe the problem or limitation you are having in your project

Callables have no defined type of parameter list

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

Add a type field to Callable, similar to Array and Dictionary

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

When defining a Callable, you can add as many type parameters as you want. These would all be valid:

# returns int
Callable[int]
# returns String, takes a float and an untyped parameter
Callable[String, float, Variant]
# returns void, takes String
Callable[void, String]
# or maybe this should be
Callable[null, String]

Lambda functions will not need type hints if types are declared in the variable or parameter, making them more concise but still type safe:

func my_method(callable: Callable[String, Node]) -> void:
    pass

# there would be code completion here, and it would give an error if it didn't return anything
my_method(func(node): return node.name)

Since actual class and method generics aren't supported (the way it's implemented in other languages), things like Array methods (map, reduce, filter) still won't work correctly.

Callable would also have methods for getting the types, like Array does:

c.get_typed_builtin(index)
c.get_typed_class_name(index)
c.get_typed_script(index)

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

It requires casting, which I think is undesirable

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

GDScript is core

@sockeye-d sockeye-d changed the title Strongly typed Callable Statically typed Callable Sep 22, 2024
@Mickeon
Copy link

Mickeon commented Sep 22, 2024

This is related to

The team on GDScript occasionally talks about this kind of thing, but it can't come any time soon due to severe changes being required to the typing system and the GDScript parser, first.

@dalexeev
Copy link
Member

dalexeev commented Sep 22, 2024

I think we should think about the unified type system as a whole, not just Callable.

See also:

@sockeye-d
Copy link
Author

Yes, I agree the implementation of "generics" as we have them seem to be a hack. I hope that the generics get a more unified system that makes it trivial to create generic classes, methods, and variables though, it would make life a lot simpler in these kinds of scenarios.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants