FAQ
Hi,

    I'm curious of the conversion of interfaces. Here is the
example http://play.golang.org/p/i0cOBhpAym
    In that example,
      interface B defines a method returning an interface A, and
      struct C implement a method returning a pointer to struct CA which
implements interface A.

    I wonder why Go can't deduce interface B from C.
    If *interface A, B* and *struct C, CA* are in different package, then
the only way to make it possible is either
     - refine the method of *C.Get* to *func Get() A*, this means I need to
import the definition of interface A from other package and introduce more
dependency, or
     - refine the method *Get* of C and B to *func Get() interface{}*

    Can anyone give me some hint or clue?



--
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

  • Ian Lance Taylor at Jul 29, 2015 at 6:50 pm

    On Wed, Jul 29, 2015 at 12:23 AM, mission liao wrote:
    I'm curious of the conversion of interfaces. Here is the example
    http://play.golang.org/p/i0cOBhpAym
    In that example,
    interface B defines a method returning an interface A, and
    struct C implement a method returning a pointer to struct CA which
    implements interface A.

    I wonder why Go can't deduce interface B from C.
    If interface A, B and struct C, CA are in different package, then the
    only way to make it possible is either
    - refine the method of C.Get to func Get() A, this means I need to
    import the definition of interface A from other package and introduce more
    dependency, or
    - refine the method Get of C and B to func Get() interface{}

    Can anyone give me some hint or clue?
    What you are looking for is called a covariant return type. As you've
    discovered, Go doesn't implement them. This both a matter of language
    policy--Go has very simple type rules, and that is a good thing--and
    implementation issues. The representation of *CA and A are quite
    different, so something would have to convert the value. It's
    possible to dynamically convert interface types at runtime, so it's
    quite possible that the compiler will never see the need to the
    conversion code. So who should generate it, and how?

    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Caleb Doxsey at Jul 30, 2015 at 4:23 am
    Just a guess, but maybe:

    Create a type:

    type CToB struct {
    *C
    }

    func (c CToB) Get() A {
    return c.C.Get()
    }


    Or:

    type CToB struct {
    C interface {
    Get() *CA
    }
    }


    And call it like this:

    func main() {
    var c interface{} = CToB{&C{}}
    d := c.(B)
    fmt.Println(d.Get().AAA())
    fmt.Println("Hello, playground")
    }



    On Wednesday, July 29, 2015 at 7:14:20 AM UTC-7, mission liao wrote:

    Hi,

    I'm curious of the conversion of interfaces. Here is the example
    http://play.golang.org/p/i0cOBhpAym
    In that example,
    interface B defines a method returning an interface A, and
    struct C implement a method returning a pointer to struct CA which
    implements interface A.

    I wonder why Go can't deduce interface B from C.
    If *interface A, B* and *struct C, CA* are in different package, then
    the only way to make it possible is either
    - refine the method of *C.Get* to *func Get() A*, this means I need
    to import the definition of interface A from other package and introduce
    more dependency, or
    - refine the method *Get* of C and B to *func Get() interface{}*

    Can anyone give me some hint or clue?

    --
    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.
  • Steven Blenkinsop at Jul 30, 2015 at 4:59 am

    On Wed, Jul 29, 2015 at 2:50 PM, Ian Lance Taylor wrote:

    It's possible to dynamically convert interface types at runtime, so
    it's quite possible that the compiler will never see the need to
    the conversion code. So who should generate it, and how?
    That's definitely a strong point. Speaking only hypothecally here (as I'm
    generally inclined to do), if this was to be supported down the road, the
    only way I can immediately see to do it would be to be able to mark the
    return value as using a representation that both sides know how to use. The
    method can at least return a fixed sized representation of the value, since
    that's interface agnostic. However, the itable of the returned value would
    have to be known at the call site. This would seem to require some notion
    of associated types:

         type B interface {
             type AType A
             GetA() self.AType
         }

         type C.AType = CA
         func (c *C) GetA() self.AType { ... }

    Of course, this is starting to look suspiciously like generics, which is a
    pretty massive feature just for the sake of having covariant return types.
    Worse, it commits to a particular implementation of generics, which means
    it could only ever be considered as part of a larger generics proposal for
    some hypothetical future major release of Go.

    On Wed, Jul 29, 2015 at 2:50 PM, Ian Lance Taylor <iant@golang.org
    wrote:
    On Wed, Jul 29, 2015 at 12:23 AM, mission liao <missionaryliao@gmail.com
    wrote:
    I'm curious of the conversion of interfaces. Here is the example
    http://play.golang.org/p/i0cOBhpAym
    In that example,
    interface B defines a method returning an interface A, and
    struct C implement a method returning a pointer to struct CA which
    implements interface A.

    I wonder why Go can't deduce interface B from C.
    If interface A, B and struct C, CA are in different package, then the
    only way to make it possible is either
    - refine the method of C.Get to func Get() A, this means I need to
    import the definition of interface A from other package and introduce more
    dependency, or
    - refine the method Get of C and B to func Get() interface{}

    Can anyone give me some hint or clue?
    What you are looking for is called a covariant return type. As you've
    discovered, Go doesn't implement them. This both a matter of language
    policy--Go has very simple type rules, and that is a good thing--and
    implementation issues. The representation of *CA and A are quite
    different, so something would have to convert the value. It's
    possible to dynamically convert interface types at runtime, so it's
    quite possible that the compiler will never see the need to the
    conversion code. So who should generate it, and how?

    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+unsubscribe@googlegroups.com
    <javascript:_e(%7B%7D,'cvml','golang-nuts%2bunsubscribe@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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJul 29, '15 at 2:14p
activeJul 30, '15 at 4:59a
posts4
users4
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase