Anonymous function with generics does not work.
https://go2goplay.golang.org/p/5DSE8QCPltv
package main
import (
"fmt"
)
func main() {
x := func(type T interface{type int, string}) (v []T) T {
max := v[0]
for _, vv := range v {
if vv > max {
max = vv
}
}
return max
}
fmt.Println(x([]int{1, 2, 3}))
fmt.Println(x([]string{"bar", "foo"}))
}
Anonymous function x works well.
Compilation error
I'm not sure the spec well but I hope this work.
This is the example with the syntax fixed: https://go2goplay.golang.org/p/5DSE8QCPltv
The correct syntax is:
...
x := func(type T interface{type int, string}) (v []T) T {
...
The error:
prog.go:8:14: function type must have no type parameters
@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 x is a variable in local scope, it should be valid to use as a value.
What would it mean to pass x to some other function as a value of type interface{}? (For example, what would reflect.TypeOf(x) report?)
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.)
I just encountered this issue and wondering if there has been any movement on this?
My use case is fairly simple: Receive a function that transforms the data given to it and returns the result. My attempt is here:
https://go2goplay.golang.org/p/_JbH_7uPrTX~~
Is there a workaround for this scenario?
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).
Most helpful comment
In this example, since
xis a variable in local scope, it should be valid to use as a value.What would it mean to pass
xto some other function as a value of typeinterface{}? (For example, what wouldreflect.TypeOf(x)report?)