Re: [golang-dev] Binding receiver object to a method expression, to get a method value

84 views
Skip to first unread message

roger peppe

unread,
Jan 18, 2017, 4:39:13 PM1/18/17
to Bryan Chan, golang-nuts, golang-dev
On 18 January 2017 at 18:45, Bryan Chan <brya...@gmail.com> wrote:
> In Go, you could pass a method value around so that the method can be
> invoked later on a pre-determined receiver, e.g.
>
> func (a *A) foo() bool { ... }
>
> func bar(f func () bool) {
> if f() {
> ...
> }
> }
>
> func main() {
> a := &A{ ... }
> bar(a.foo)
> }
>
>
> You could also create a method expression so that the receiver can be
> supplied at actual call time, e.g.
>
> f := (*A).foo
> boolVal := f(a)
>
>
> But I couldn't find a way to bind a receiver to a method expression, such
> that the resulting method value can be invoked at a later time. Would this
> be a useful addition to the language? Or did I miss something?

If a is an object with the method foo, a.foo will give you a function that
will invoke the method at a later time.

See https://golang.org/ref/spec#Method_values

>
> --
> Bryan
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-dev" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-dev+...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Bryan Chan

unread,
Jan 18, 2017, 5:08:47 PM1/18/17
to golang-nuts, golan...@googlegroups.com
In Go, you could pass a method value around so that the method can be invoked later on a pre-determined receiver, e.g.

func (a *A) foo() bool { ... } 

func bar(f func () bool) {
    if f() {
        ...
    }
}

func main() {
    a := &A{ ... }
    bar(a.foo)
}

You could also create a method expression so that the receiver can be supplied at actual call time, e.g.

f := (*A).foo
boolVal := f(a)

But I couldn't find a way to bind a receiver to a method expression, such that the resulting method value can be invoked at a later time. Would this be a useful addition to the language? Or did I miss something?

--
Bryan

Bryan Chan

unread,
Jan 18, 2017, 9:55:17 PM1/18/17
to golang-dev, brya...@gmail.com, golan...@googlegroups.com
On Wednesday, 18 January 2017 16:39:03 UTC-5, rog wrote:
On 18 January 2017 at 18:45, Bryan Chan <brya...@gmail.com> wrote:
> But I couldn't find a way to bind a receiver to a method expression, such
> that the resulting method value can be invoked at a later time. Would this
> be a useful addition to the language? Or did I miss something?

If a is an object with the method foo, a.foo will give you a function that
will invoke the method at a later time.

See https://golang.org/ref/spec#Method_values
 
I understand what method values are. My original email already contained an example. What I want to do is to store away a method expression, and then over time, bind that expression with different receivers to form different method values, so that those method values can be passed to other parts of the program and invoked later.

--
Bryan

Tamás Gulácsi

unread,
Jan 19, 2017, 12:06:13 AM1/19/17
to golang-nuts
No, but you can create a function/closure which gets the receiver as first arg, and either executes the method with the given args, or returns such a function, as you wish.

minux

unread,
Jan 19, 2017, 12:13:08 AM1/19/17
to Bryan Chan, golang-dev, golang-nuts
It's possible to use reflect.MakeFunc to bind a argument to a func.
The result won't be very efficient though, so unless you really want to be generic, you probably should create a closure for this statically (which means you must know the exact type of the method).

Jordan Krage

unread,
Jan 19, 2017, 1:54:59 PM1/19/17
to golang-dev, brya...@gmail.com, golan...@googlegroups.com
Would a simple single method interface meet your needs?

type fooer interface {
    foo() bool
}

func (a *A) foo() bool { ... } 

func bar(f fooer) {
    if f.foo() {
        ...
    }
}

func main() {
    a := &A{ ... }
    bar(a)
}
Reply all
Reply to author
Forward
0 new messages