While developing GoDerive, a code generator for Go, I ran into the following problem.
I could not infer the type of the input arguments of a function, if that function's input argument types are not specified and the function is not being called right away.
func twoslice(a, b []int, f func(c, d []int) int) {
f(a, b)
}
func main() {
a, b := []int{1,2}, []int{3, 4}
twoslice(a, b, copy)
fmt.Println("%v, %v", a, b)
}
Generic functions cannot be passed as values
While developing GoDerive, a code generator for Go, I ran into the following problem.
I could not infer the type of the input arguments of a function, if that function's input argument types are not specified and the function is not being called right away.
I wanted to write:
, where functions that start with the prefix "derive" are generated.
This can be fixed by rewriting it as:
, but this is not ideal.
I tried to find how Go does this for it's generic functions, like
copy
, but I found that this limitation is actually part of the Go specification:https://golang.org/ref/spec#Built-in_functions
Here is an example to showcase the limitation:
https://play.golang.org/p/ghITepOq6l
This gives the following error:
I am not saying this is an easy problem to solve, but it stops GoDerive from generating functions that can be passed as values.
In the meantime I have updated my code generator's deriveEqual function to be able to generate, given only the first argument.
This allows us to now write:
, but this will not be possible for all such usecases.
This is just my experience of trying to generate generic functions in Go and one of the limitations I ran into.