On 18 January 2017 at 18:45, Bryan Chan <bryan...@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+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

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

Reply via email to