Skip to content

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

Closed
bradfitz opened this issue May 1, 2011 · 8 comments
Closed

reflect: add MakeFunc #1765

bradfitz opened this issue May 1, 2011 · 8 comments
Milestone

Comments

@bradfitz
Copy link
Contributor

bradfitz commented May 1, 2011

Feature request:

reflect.MakeFunc(Type, func([]Value) []Value)

So one can do:

  var f func(a, b int) string
  embeddedLang.GetFunc("func", &f)
  s := f(1, 2)
@bradfitz
Copy link
Contributor Author

bradfitz commented May 1, 2011

Comment 1:

with a return value of course:
reflect.MakeFunc(Type, func([]Value) []Value) Value

@robpike
Copy link
Contributor

robpike commented Jul 26, 2011

Comment 2:

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))
}

@rsc
Copy link
Contributor

rsc commented Jul 26, 2011

Comment 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.

@bradfitz
Copy link
Contributor Author

Comment 4:

Rob, I want this for more natural APIs when embedding scripting languages in Go.

@gopherbot
Copy link
Contributor

Comment 5 by m@capitanio.org:

+1 As I see it, that could simplify a lot of my Go code too.

@rsc
Copy link
Contributor

rsc commented Dec 9, 2011

Comment 6:

Labels changed: added priority-later, removed priority-medium.

@rsc
Copy link
Contributor

rsc commented Sep 12, 2012

Comment 8:

Labels changed: added go1.1.

@rsc
Copy link
Contributor

rsc commented Sep 25, 2012

Comment 9:

This issue was closed by revision ba4625c.

Status changed to Fixed.

@rsc rsc added this to the Go1.1 milestone Apr 14, 2015
@rsc rsc removed the go1.1 label Apr 14, 2015
@golang golang locked and limited conversation to collaborators Jun 24, 2016
@rsc rsc removed their assignment Jun 22, 2022
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants