Generic 使用反射检查分配给接口{}变量的函数的签名
我正在尝试构建一个如下所示的函数:Generic 使用反射检查分配给接口{}变量的函数的签名,generic,currying,reflection,go,Generic,Currying,Reflection,Go,我正在尝试构建一个如下所示的函数: package curry import ( "fmt" "reflect" ) // Function type fn interface{} // Function parameter type pr interface{} // It return the curried function func It(f fn, p ...pr) (fn, error) { // examine the concret type of
package curry
import (
"fmt"
"reflect"
)
// Function
type fn interface{}
// Function parameter
type pr interface{}
// It return the curried function
func It(f fn, p ...pr) (fn, error) {
// examine the concret type of the function f
if reflect.ValueOf(f).Kind() == reflect.Func {
// Get the slice of input and output parameters type
} else {
return nil, fmt.Errorf("%s", "takes a function as a first parameter")
}
// _, _ = f, p
return nil, nil
}
是否可以将输入和输出参数类型的切片提取为函数
f
的[]reflect.Type
?您可以使用reflect.Type.In(int)
和reflect.Type.Out(int)
,有相应的方法称为NumIn()int
和NumOut()int
提供输入/输出的数量
但是,请记住一些注意事项:
如果你一定要做这样的事情,我强烈建议你制作一个接口,让每个实现自己动手,而不是试图“一般”地对所有情况进行破解,这在Go 1.2.1中永远不会起作用。你可以使用
reflect.Type.In(int)
和reflect.Type.Out(int)
,有相应的方法称为NumIn()int
和NumOut()int
,它们为您提供输入/输出的数量
但是,请记住一些注意事项:
如果你一定要做这样的事情,我强烈建议你制作一个接口,让每个实现自己动手,而不是试图“一般”地对所有情况进行破解,这在Go 1.2.1中永远不会起作用。Go 1.5将添加一个功能,在这里可能会有所帮助。 (,by) 测试用例包括以下有趣的代码:
v := MakeFunc(FuncOf([]Type{TypeOf(K(""))}, []Type{TypeOf(V(0))}, false), fn)
outs := v.Call([]Value{ValueOf(K("gopher"))})
Go 1.5将在此处添加一个有帮助的函数。 (,by) 测试用例包括以下有趣的代码:
v := MakeFunc(FuncOf([]Type{TypeOf(K(""))}, []Type{TypeOf(V(0))}, false), fn)
outs := v.Call([]Value{ValueOf(K("gopher"))})