You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
functwoslice(a, b []int, ffunc(c, d []int) int) {
f(a, b)
}
funcmain() {
a, b:= []int{1,2}, []int{3, 4}
twoslice(a, b, copy)
fmt.Println("%v, %v", a, b)
}
awalterschulze
changed the title
derive higher order functions with input functions that should also be derived.
Go Experience Report: Generic functions cannot be passed as values
Sep 24, 2017
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.
The text was updated successfully, but these errors were encountered: