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
cmd/go2go: anonymous func with generics #39632
Comments
This is the example with the syntax fixed: https://go2goplay.golang.org/p/5DSE8QCPltv ...
x := func(type T interface{type int, string}) (v []T) T {
... The error:
|
@fhs Thanks. Updated summary. |
CC @griesemer My first take on this is that this is a bad error message from the parser. At least at the moment, generic function literals are not supported. It might be nice to support them just for orthogonality. I don't see a convincing reason why we need them, but I also don't see a convincing reason why we can't implement them. That said, even if we fix the parser and type checker to handle them, they won't be supported by the translation tool. |
Sure, it is easy to accept type parameters in function literal syntax. Edited: The parser already accepts type parameters in function literals, but then reports an error (that error could perhaps be clearer). For the reasons discussed below we had decided that it's not super useful to permit generic function literals. |
In this example, since What would it mean to pass |
I agree with @bcmills that this does seem fairly problematic. I think the only thing you could do with a generic function literal is instantiate it or call it. That's not completely useless...but it's almost entirely useless. |
Function literals stored in variables are currently the only way to have function-scoped functions. I often use function-scoped function variables to reduce code redundancy without polluting the package scope. I would use macros instead if we had that. Generics are kind of strongly typed macros. I would be sad if I couldn't use generics to define a function inside a function. |
@dolmen, it would arguably be more consistent to allow a function declaration at local scope, which could then be parameterized. (A function declaration — unlike a function literal — is not a value, so the questions about the type of that value would be moot.) |
EDIT: I just got this working. Hopefully this will help others: https://go2goplay.golang.org/p/qpZOGSAwxEG |
Yes, that's one way to solve this. Btw., here's the updated example code: https://go2goplay.golang.org/p/mUWfsZPHs5h (now working with the latest type parameter syntax). |
If they can't declare type parameters, will anonymous functions be allowed to use in-scope type parameters? (they are allowed in go2go):
|
@dnmfarrell Yes, that is fine. |
|
Anonymous function with generics does not work.
What did you do?
https://go2goplay.golang.org/p/5DSE8QCPltv
What did you expect to see?
Anonymous function x works well.
What did you see instead?
Compilation error
I'm not sure the spec well but I hope this work.
The text was updated successfully, but these errors were encountered: