FAQ
This may be basic, but it's had me stumped for a while now. I'm new to Go.

This code will not compile: http://play.golang.org/p/06FQ0oqFIF

It fails with "prog.go:23: cannot use SomeFunc (type func(Bar)) as type
AnyFunc in function argument"

I suspect the problem is that Bar is not of interface{} type. But my
understanding is that everything satisfies the empty interface. I tried
various coercions and assertions, but can't figure out how to get past
this. BTW, the code is a stripped down, illustrative example of my real
code.

Any help would be appreciated.

Thanks,
Pete

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Shawn Milochik at Apr 16, 2014 at 4:16 pm
    The thing is that SomeFunc isn't actually an AnyFunc -- they have different
    signatures.

         An AnyFunc is a function that accepts an empty interface and returns
    nothing.
         A SomeFunc is a function that accepts a Bar and returns nothing.

    So, SomeFunc does not satisfy the signature of 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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • DV at Apr 16, 2014 at 4:18 pm
    AnyFunc says "I'm a function that can take any kind of parameter". It is
    *not* saying "I'm any function".

    On Wednesday, April 16, 2014 10:11:08 AM UTC-6, Peter Lacey wrote:

    This may be basic, but it's had me stumped for a while now. I'm new to Go.

    This code will not compile: http://play.golang.org/p/06FQ0oqFIF

    It fails with "prog.go:23: cannot use SomeFunc (type func(Bar)) as type
    AnyFunc in function argument"

    I suspect the problem is that Bar is not of interface{} type. But my
    understanding is that everything satisfies the empty interface. I tried
    various coercions and assertions, but can't figure out how to get past
    this. BTW, the code is a stripped down, illustrative example of my real
    code.

    Any help would be appreciated.

    Thanks,
    Pete
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Shawn Milochik at Apr 16, 2014 at 4:26 pm

    On Wed, Apr 16, 2014 at 12:18 PM, DV wrote:

    AnyFunc says "I'm a function that can take any kind of parameter". It is
    *not* saying "I'm any function".
    Thanks for that clarification. I was just trying to figure out how that
    worked myself. I knew what the error was, but not the distinction between
    "an empty interface can be anything" and "a function that accepts n empty
    interfaces != a function that accepts n of arbitrary types."

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Nate Finch at Apr 16, 2014 at 4:49 pm
    The thing about interfaces is that they are not simply pattern matching for
    the compiler. They're actual types and values in your code.

    type AnyFunc func(interface{})

    This says "create a new type, called AnyFunc, which should act like a
    function that takes an empty interface as its parameter, except treat it as
    a separate type."

    It is not an alias for "any function with a single argument". It's not
    even an alias for "any function that takes an empty interface". It is a
    separate type, the way float and int are separate types. You *can* convert
    appropriately defined functions into an AnyFunc, but they're not that type
    by default.

    Now, that being said, you *can* pass any argument you want into an AnyFunc,
    so you can do

    func RunIt(f AnyFunc) {
          f("foobar")
          f(52)
          // ... etc
    }

    To pass a function into RunIt above, you need specifically pass a function
    that is of type AnyFunc (which generally will require a type conversion).

    func MyFunc(i interface{}) { fmt.Println(i) }

    // type convert MyFunc into an AnyFunc
    a := AnyFunc(MyFunc)
    RunIt(a)

    Note that even though MyFunc exactly fits the definition of AnyFunc, it is
    of a different type, so you can't just pass it in to RunIt, you have to
    convert it to an AnyFunc first.

    Hope that helps.
    On Wednesday, April 16, 2014 12:25:54 PM UTC-4, Shawn Milochik wrote:

    On Wed, Apr 16, 2014 at 12:18 PM, DV <dimiter....@gmail.com <javascript:>>wrote:
    AnyFunc says "I'm a function that can take any kind of parameter". It is
    *not* saying "I'm any function".
    Thanks for that clarification. I was just trying to figure out how that
    worked myself. I knew what the error was, but not the distinction between
    "an empty interface can be anything" and "a function that accepts n empty
    interfaces != a function that accepts n of arbitrary types."

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Peter Lacey at Apr 16, 2014 at 4:58 pm
    Thanks, Nate, it's beginning to crystallize. That said, if you look at my
    later example, when I change main to convert SpeakFunc to a BarkerFunc, I
    get "cannot convert SpeakFunc (type func(Dog)) to type BarkerFunc." So the
    problem lies (also?) with the fact that Dog is not a Barker, it's just a
    Dog. Correct? Despite the fact that it implements the Barker interface.

    -- Pete
    On Wednesday, April 16, 2014 12:49:23 PM UTC-4, Nate Finch wrote:

    The thing about interfaces is that they are not simply pattern matching
    for the compiler. They're actual types and values in your code.

    type AnyFunc func(interface{})

    This says "create a new type, called AnyFunc, which should act like a
    function that takes an empty interface as its parameter, except treat it as
    a separate type."

    It is not an alias for "any function with a single argument". It's not
    even an alias for "any function that takes an empty interface". It is a
    separate type, the way float and int are separate types. You *can* convert
    appropriately defined functions into an AnyFunc, but they're not that type
    by default.

    Now, that being said, you *can* pass any argument you want into an
    AnyFunc, so you can do

    func RunIt(f AnyFunc) {
    f("foobar")
    f(52)
    // ... etc
    }

    To pass a function into RunIt above, you need specifically pass a function
    that is of type AnyFunc (which generally will require a type conversion).

    func MyFunc(i interface{}) { fmt.Println(i) }

    // type convert MyFunc into an AnyFunc
    a := AnyFunc(MyFunc)
    RunIt(a)

    Note that even though MyFunc exactly fits the definition of AnyFunc, it is
    of a different type, so you can't just pass it in to RunIt, you have to
    convert it to an AnyFunc first.

    Hope that helps.
    On Wednesday, April 16, 2014 12:25:54 PM UTC-4, Shawn Milochik wrote:
    On Wed, Apr 16, 2014 at 12:18 PM, DV wrote:

    AnyFunc says "I'm a function that can take any kind of parameter". It is
    *not* saying "I'm any function".
    Thanks for that clarification. I was just trying to figure out how that
    worked myself. I knew what the error was, but not the distinction between
    "an empty interface can be anything" and "a function that accepts n empty
    interfaces != a function that accepts n of arbitrary types."

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Peter Lacey at Apr 16, 2014 at 4:51 pm
    I'm sorry, it's still not gelling with me. I think the empty interface
    might be a red herring. Here's an example that's closer to my real world
    code.

    http://play.golang.org/p/0Eyuv-Bae4

    In this case a BarkerFunc is a type of function that takes a Barker,
    SpeakFunc is an implementation of the type BarkerFunc, and Speak is a
    function that takes a BarkerFunc. Any yet, when I call Speak(SpeakFunc) I
    get the error: "cannot use SpeakFunc (type func(Dog)) as type BarkerFunc in
    function argument"

    I suspect what I'm hearing here is that a "SpeakFunc (type func(Dog))" is
    not a "BarkerFunc" it's a "SpeakFunc (type func(Dog))." If so, then I'm
    lost on the relationship between function type declarations and the
    functions that honor them.

    Or maybe it's the case that the Dog type is not a Barker. And in that case
    I'm lost of the distinction between an interface declaration and the
    objects that honor them.

    Sorry if I'm being dense.

    -- Pete


    On Wednesday, April 16, 2014 12:11:08 PM UTC-4, Peter Lacey wrote:

    This may be basic, but it's had me stumped for a while now. I'm new to Go.

    This code will not compile: http://play.golang.org/p/06FQ0oqFIF

    It fails with "prog.go:23: cannot use SomeFunc (type func(Bar)) as type
    AnyFunc in function argument"

    I suspect the problem is that Bar is not of interface{} type. But my
    understanding is that everything satisfies the empty interface. I tried
    various coercions and assertions, but can't figure out how to get past
    this. BTW, the code is a stripped down, illustrative example of my real
    code.

    Any help would be appreciated.

    Thanks,
    Pete
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Nate Finch at Apr 16, 2014 at 5:01 pm
    It's understandable. The whole "define a new type that looks like another
    type" is pretty unique to Go.

    Let me try to clarify my other message (though I assume you didn't read it
    yet, since we posted about the same time).

    The new type and the type it is defined from are as different to the
    compiler as a float and an int. You can cast one to the other, but there's
    no automatic conversion.

    Here's the fix and the explanation: http://play.golang.org/p/dvx80m2n1T



    On Wednesday, April 16, 2014 12:51:01 PM UTC-4, Peter Lacey wrote:

    I'm sorry, it's still not gelling with me. I think the empty interface
    might be a red herring. Here's an example that's closer to my real world
    code.

    http://play.golang.org/p/0Eyuv-Bae4

    In this case a BarkerFunc is a type of function that takes a Barker,
    SpeakFunc is an implementation of the type BarkerFunc, and Speak is a
    function that takes a BarkerFunc. Any yet, when I call Speak(SpeakFunc) I
    get the error: "cannot use SpeakFunc (type func(Dog)) as type BarkerFunc in
    function argument"

    I suspect what I'm hearing here is that a "SpeakFunc (type func(Dog))" is
    not a "BarkerFunc" it's a "SpeakFunc (type func(Dog))." If so, then I'm
    lost on the relationship between function type declarations and the
    functions that honor them.

    Or maybe it's the case that the Dog type is not a Barker. And in that
    case I'm lost of the distinction between an interface declaration and the
    objects that honor them.

    Sorry if I'm being dense.

    -- Pete


    On Wednesday, April 16, 2014 12:11:08 PM UTC-4, Peter Lacey wrote:

    This may be basic, but it's had me stumped for a while now. I'm new to
    Go.

    This code will not compile: http://play.golang.org/p/06FQ0oqFIF

    It fails with "prog.go:23: cannot use SomeFunc (type func(Bar)) as type
    AnyFunc in function argument"

    I suspect the problem is that Bar is not of interface{} type. But my
    understanding is that everything satisfies the empty interface. I tried
    various coercions and assertions, but can't figure out how to get past
    this. BTW, the code is a stripped down, illustrative example of my real
    code.

    Any help would be appreciated.

    Thanks,
    Pete
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Peter Lacey at Apr 16, 2014 at 5:08 pm
    Thank you! Thank you! Thank you! The scales have fallen from my eyes!

    -- Pete
    On Wednesday, April 16, 2014 1:01:06 PM UTC-4, Nate Finch wrote:

    It's understandable. The whole "define a new type that looks like
    another type" is pretty unique to Go.

    Let me try to clarify my other message (though I assume you didn't read it
    yet, since we posted about the same time).

    The new type and the type it is defined from are as different to the
    compiler as a float and an int. You can cast one to the other, but there's
    no automatic conversion.

    Here's the fix and the explanation: http://play.golang.org/p/dvx80m2n1T



    On Wednesday, April 16, 2014 12:51:01 PM UTC-4, Peter Lacey wrote:

    I'm sorry, it's still not gelling with me. I think the empty interface
    might be a red herring. Here's an example that's closer to my real world
    code.

    http://play.golang.org/p/0Eyuv-Bae4

    In this case a BarkerFunc is a type of function that takes a Barker,
    SpeakFunc is an implementation of the type BarkerFunc, and Speak is a
    function that takes a BarkerFunc. Any yet, when I call Speak(SpeakFunc) I
    get the error: "cannot use SpeakFunc (type func(Dog)) as type BarkerFunc in
    function argument"

    I suspect what I'm hearing here is that a "SpeakFunc (type func(Dog))" is
    not a "BarkerFunc" it's a "SpeakFunc (type func(Dog))." If so, then I'm
    lost on the relationship between function type declarations and the
    functions that honor them.

    Or maybe it's the case that the Dog type is not a Barker. And in that
    case I'm lost of the distinction between an interface declaration and the
    objects that honor them.

    Sorry if I'm being dense.

    -- Pete


    On Wednesday, April 16, 2014 12:11:08 PM UTC-4, Peter Lacey wrote:

    This may be basic, but it's had me stumped for a while now. I'm new to
    Go.

    This code will not compile: http://play.golang.org/p/06FQ0oqFIF

    It fails with "prog.go:23: cannot use SomeFunc (type func(Bar)) as type
    AnyFunc in function argument"

    I suspect the problem is that Bar is not of interface{} type. But my
    understanding is that everything satisfies the empty interface. I tried
    various coercions and assertions, but can't figure out how to get past
    this. BTW, the code is a stripped down, illustrative example of my real
    code.

    Any help would be appreciated.

    Thanks,
    Pete
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Nigel Tao at Apr 16, 2014 at 7:49 pm

    On Wed, Apr 16, 2014 at 9:51 AM, Peter Lacey wrote:
    I'm sorry, it's still not gelling with me. I think the empty interface
    might be a red herring. Here's an example that's closer to my real world
    code.

    http://play.golang.org/p/0Eyuv-Bae4
    I think that the empty interface is a red herring, and also the fact
    that you can declare new types like
    type FooFunc func(Foo)
    is a red herring.

    The thing is that
    func(Foo)
    is a different type from
    func(Bar)
    even if Foo (a concrete type) implements Bar (an interface type).
    Example: http://play.golang.org/p/ekYYCDY10Z

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedApr 16, '14 at 4:11p
activeApr 16, '14 at 7:49p
posts10
users5
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase