[go-nuts] go: Passing a member function as an argument compile error
I just posted this on on openstack. Maybe someone here knows the answer? https://stackoverflow.com/questions/55410250/go-passing-a-member-function-as-an-argument-compile-error I would expect this to compile after watching this talk by Rob Pike https://www.youtube.com/watch?v=HxaD_trXwRE&t=1830s package main type StateFunc func(*M) StateFunc type M struct { start StateFunc } func New(start StateFunc) *M { return &M{ start: start, } } func (m *M) foo() StateFunc { return nil } func (m *M) Start() { go m.run() } func (m *M) run() { state := m.start for state != nil { state = state(m) } } func main() { m := New(foo) } It fails with prog.go:31:14: undefined: foo -- 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.
Re: [go-nuts] go: Passing a member function as an argument compile error
No, I intended to call New(foo) -- 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.
Re: [go-nuts] go: Passing a member function as an argument compile error
Thanks! That compiles. On Thursday, March 28, 2019 at 10:14:28 PM UTC-7, Ian Lance Taylor wrote: > > On Thu, Mar 28, 2019 at 10:02 PM Greg Bostrom > wrote: > > > > No, I intended to call New(foo) > > Well, in the program you showed us, foo is not defined, and that is > what the compiler is telling you. > > It was mistaken in thinking that you meant new(M), though, since you > actually want to call New. What you want is most likely > m := New((*M).foo) > > Ian > -- 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.