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

Explicit generic function arguments #724

Open
svermeulen opened this issue Dec 9, 2023 · 4 comments
Open

Explicit generic function arguments #724

svermeulen opened this issue Dec 9, 2023 · 4 comments
Labels
feature request New feature or request semantics Unexpected or unsound behaviors

Comments

@svermeulen
Copy link
Contributor

Sometimes, the type that is used by a generic function is not passed in implicitly by the arguments. These situations would benefit from having a way to explicitly tell the teal compiler what type should be used for a given generic argument.

For eg: local foo = get_foo::<integer>()

This was previously mentioned as something to consider here but couldn't find an issue for it (if I missed this please close)

@hishamhm
Copy link
Member

Teal does bidirectional type inference to try to avoid the need for this.

In your example above, doing local foo: integer = get_foo() should make the expected type flow into the function call.

Do you have a more complex example where that doesn't work and the explicit generic function argument would still be necessary? I've been trying to see how far we can go without adding more syntax for function calls.

@hishamhm hishamhm added feature request New feature or request semantics Unexpected or unsound behaviors labels Dec 11, 2023
@svermeulen
Copy link
Contributor Author

So I went back to the case that prompted this request, and found that I was able to avoid the need for an explicit generic by adding more locals with explicit types. There might be some cases where this workaround isn't sufficient but I can't find it at the moment.

@hishamhm
Copy link
Member

There might be some cases where this workaround isn't sufficient but I can't find it at the moment.

I honestly don't see it as a workaround, since it amounts to the same number of annotations. I understand that one gets the feeling that ideally no variables should need to be annotated, but if anything, I find that not needing a new (pseudo-?)operator is a cleaner approach, language-wise.

Explicit variable annotations and bidirectional inference do a lot of heavy lifting to resolve the types of things like literal tables (e.g. local t: MyComplexRecord = { ... { ... { ... deeply_nested_thing = ... } ... } ... }), and this was the primary reason why we added bidirectional inference. Being able to use the same machinery to avoid the need for the "turbofish annotation" felt like a good way to go. The present rule is that variables for function calls are resolved either by the expected types of its return values in the given call-site context, or the types of its given arguments.

@Hedwig7s
Copy link

I think this could be useful in situations where the type is abstract (mainly interfaces)
If you want a function to return something of type T extends X you could call the function using funcname()
To avoid turbofish syntax typescript syntax can be used (as shown previously)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature or request semantics Unexpected or unsound behaviors
Projects
None yet
Development

No branches or pull requests

3 participants