FAQ
How does this differ from
https://groups.google.com/forum/?fromgroups=#!topic/golang-nuts/PosV49dhKCA[1-25]?
On Wednesday, August 29, 2012 2:37:07 AM UTC-7, Erik Unger wrote:

I think it would be useful to extend the language to allow binding
of object + method as a function value that can be called without having to
pass the object as first argument.

Why? I have seen multiple instances of reflection being used to wrap
method calls by using an object pointer and the method name as string. This
dynamic behavior wouldn't be necessary, if bound methods and functions
could be called with the same interface.

reflect.Value.Method() already does method binding, why not have it in the
language?

Of course, a binary function pointer compatible implementation would be
tricky, but not impossible.
The function code that prepends the address of the object to argument
list could get generated on the fly when an object+method gets assigned to
a function pointer.

-Erik

Search Discussions

  • Peter S at Aug 31, 2012 at 10:03 pm
    The "Pointer to method" in the linked thread represents the method of the
    type as a function which can be called with any instance. That is, the
    receiver instance has to be passed as the first parameter when invoking the
    pointed function. Example code by Ian Lance Taylor from the linked thread
    (comment mine):

    type Foo struct{}
    func (f *Foo) Method() {
    fmt.Println("Method")
    }

    func main() {
    var foo Foo
    fn := (*Foo).Method
    fn(&foo) // receiver instance passed as parameter
    }

    The feature proposed in Issue
    2280<https://code.google.com/p/go/issues/detail?id=2280>and in this
    thread would represent the method of a specific instance as a
    function value. That is, the receiver instance is decided when the function
    value is obtained, and the caller of the function value only provides the
    method parameters. An example based on the previous one would be:

    func main() {
    var foo Foo
    fn := foo.Method // not valid in Go1
    fn() // receiver is "foo", no parameters needed
    }

    In Go1, the equivalent can be achieved using a closure. It is short in this
    example, but it gets longer and more difficult to read when the method has
    more parameters:

    func main() {
    var foo Foo
    fn := func() { foo.Method() }
    fn()
    }

    Peter

    On Fri, Aug 31, 2012 at 11:41 PM, Miki Tebeka wrote:

    How does this differ from
    https://groups.google.com/forum/?fromgroups=#!topic/golang-nuts/PosV49dhKCA[1-25]
    ?

    On Wednesday, August 29, 2012 2:37:07 AM UTC-7, Erik Unger wrote:

    I think it would be useful to extend the language to allow binding
    of object + method as a function value that can be called without having to
    pass the object as first argument.

    Why? I have seen multiple instances of reflection being used to wrap
    method calls by using an object pointer and the method name as string. This
    dynamic behavior wouldn't be necessary, if bound methods and functions
    could be called with the same interface.

    reflect.Value.Method() already does method binding, why not have it in
    the language?

    Of course, a binary function pointer compatible implementation would be
    tricky, but not impossible.
    The function code that prepends the address of the object to argument
    list could get generated on the fly when an object+method gets assigned to
    a function pointer.

    -Erik

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedAug 31, '12 at 2:46p
activeAug 31, '12 at 10:03p
posts2
users2
websitegolang.org

2 users in discussion

Miki Tebeka: 1 post Peter S: 1 post

People

Translate

site design / logo © 2022 Grokbase