Functional parameters in Go functions

67 views Asked by At

Is there a way to pass a function(which can be generic) to another function? I know that with known input types and return types we can pass a function but a generic approach is needed

1

There are 1 answers

0
VonC On

When reading the Go2 proposal on generic: "Type Parameters - Draft Design", I am not sure you would be able to pass as parameter a generic function without explicitly specify the type used by said function

See "Instantiating a function"

Go normally permits you to refer to a function without passing any arguments, producing a value of function type.
You may not do this with a function that has type parameters; all type arguments must be known at compile time.
That said, you can instantiate the function, by passing type arguments, but you don't have to call the instantiation. This will produce a function value with no type parameters.

// PrintInts is type func([]int).
var PrintInts = Print[int]