FAQ
Here is an example of code I would like to work in Go. Unfortunately
interfaces in Go are only implemented by functions with exactly the same
return types.
Why is this not supported and will it ever be supported?

package main

// the interface function returns another interface
type interfaceReturner interface {
     getOtherInterface() otherInterface
}
type otherInterface interface{}

// this implementation fits the interface and returns exactly the
otherInterface
type interfaceReturnerImplementation struct{}
func (interfaceReturnerImplementation) getOtherInterface() otherInterface {
return 0 }

// this one returns an integer (int fulfills otherInterface)
// but this does not fulfill interfaceReturner - I think it should.
type implementationReturner struct{}
func (implementationReturner) getOtherInterface() int { return 0 }

func main() {
     var _ interfaceReturner = interfaceReturnerImplementation{}
     var _ interfaceReturner = implementationReturner{} // <-- error!
}


In this case one could say that a function that returns an int can be used whereever you would need a function that reutrns 'otherInterface' because int fulfills 'otherInterface'.

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

  • Flicaflow at Jun 24, 2015 at 1:23 pm
    Hi,
    it seems extremely unlikely to me that this code would ever work wit go.
    It would mean that the compiler automatically replaces the return type int
    with the matching type in order to fulfill the interface (how does it know
    which one?) which is something which go is generally not doing (no auto
    conversion / type promotion) for good reasons.

    Also in my point of view you would not win anything, consider following
    case:
    func foo(x interfaceReturner) {
       a := x.getOtherInterface()
       // which type has a ?
       // ...
       var i int = a.(int)
       // you have to type assert anyway to access the int
    }

    Also if x is actually your implementationReturner go has no way to to
    choose another type for a instead of otherInterface and you have to type
    assert anyway if you want to assess the actual return type.

    hope that helps.

    Am Mittwoch, 24. Juni 2015 10:02:50 UTC+2 schrieb gonu...@gmail.com:
    Here is an example of code I would like to work in Go. Unfortunately
    interfaces in Go are only implemented by functions with exactly the same
    return types.
    Why is this not supported and will it ever be supported?

    package main

    // the interface function returns another interface
    type interfaceReturner interface {
    getOtherInterface() otherInterface
    }
    type otherInterface interface{}

    // this implementation fits the interface and returns exactly the
    otherInterface
    type interfaceReturnerImplementation struct{}
    func (interfaceReturnerImplementation) getOtherInterface() otherInterface
    { return 0 }

    // this one returns an integer (int fulfills otherInterface)
    // but this does not fulfill interfaceReturner - I think it should.
    type implementationReturner struct{}
    func (implementationReturner) getOtherInterface() int { return 0 }

    func main() {
    var _ interfaceReturner = interfaceReturnerImplementation{}
    var _ interfaceReturner = implementationReturner{} // <-- error!
    }


    In this case one could say that a function that returns an int can be used whereever you would need a function that reutrns 'otherInterface' because int fulfills 'otherInterface'.
    --
    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.
  • Paraiso Marc at Jun 24, 2015 at 7:16 pm
    Imho 2 interfaces should be equal if they at least have the same signature
    irregardless of their name, in a "recursive" way.

    type Fooer interface{
          Foo(string)
    }

    type Barer interface{
          Foo(string)
    }

    type Bazer interface{
       Fooer
       Do()Bazer
    }

    type Bizer interface{
       Barer
       Do()Bizer
    }

    func Accept(b Bizer){}



    Accept should be able to accept either Bazer or Bizer

    --
    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.
  • Ian Lance Taylor at Jun 24, 2015 at 10:20 pm

    On Tue, Jun 23, 2015 at 11:30 PM, wrote:
    Here is an example of code I would like to work in Go. Unfortunately
    interfaces in Go are only implemented by functions with exactly the same
    return types.
    Why is this not supported and will it ever be supported?
    Short version: because the representation of two different interface
    types is different. Someone has to convert the types. The nature of
    Go is such that the types to convert from and to may not be known
    until run time. Who is going to create the conversion code, and how?

    Also: the Go type system is very simple and easy to understand. This
    change would make it much more complex.

    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.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJun 24, '15 at 8:02a
activeJun 24, '15 at 10:20p
posts4
users4
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase