-
Notifications
You must be signed in to change notification settings - Fork 18k
reflect: add MakeFunc #1765
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Labels
Milestone
Comments
This is just some code. I can't see what it's supposed to do. How does it differ from calling Interface() on a Func Value? package main import "fmt" import "reflect" func f(a int) string {return fmt.Sprint(a) } func main() { v := reflect.ValueOf(f) fmt.Printf("%T\n", v.Interface()) g := v.Interface().(func(a int)string) fmt.Println(g(3)) } |
Here's a concrete example: func genericSwap(x []reflect.Value) []reflect.Value { return []reflect.Value{x[1], x[0]} } func MakeSwap(v interface{}) { // v is a *func(t1, t2)(t2, t1) vv := reflect.ValueOf(v).Elem() vv.Set(reflect.MakeFunc(v.Type(), genericSwap)) } and then a caller can write var f func(int, int) (int, int) otherpkg.MakeSwap(&f) println(f(1,2)) // 2, 1 var g func(string, int) (int, string) otherpkg.MakeSwap(&g) println(g("hello", 42)) // 42, "hello" This is a dumb but I hope illustrative example. The point is that MakeFunc turns a function with a reflecty type signature into one with a specific concrete signature, so that you can use it with things that don't know/care about reflect. It's for creating Func Values where there wasn't previously a function. |
Comment 5 by m@capitanio.org: +1 As I see it, that could simplify a lot of my Go code too. |
This issue was closed by revision ba4625c. Status changed to Fixed. |
This issue was closed.
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
The text was updated successfully, but these errors were encountered: