FAQ
If you have the following library:

type Command interface {
Run()(i*nterface {}*, error)
}

func Execute(command Command)(*interface{},* error){
return command.Run()
}

and the client tries to use it like this:

type FirstCommand struct {}
func (_ FirstCommand) Run()(*int*, error) {
return 0, errors.New("oh oh,... service is returning an error")
}

... the compiler protests that i am not implementing the interface (have (
*int,error) *but want* (interface{}, error)*

So my question is: is there any way to do the above i.e. declaring
arbitrary return types for interfaces and not forcing the clients of my
library to write code like this?
func (_ FirstCommand) Run()(*interface{}*, error) { // <--- this would fix
it but it does not look right to me.
return 0, errors.New("oh oh,... service is returning an error")
}


Thanks in advance

JTeso







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

  • Peter Waller at Sep 19, 2014 at 12:56 pm
    In go, a function returning an integer and a function returning a
    interface{} value are not interchangeable. The functions themselves are
    different types (and are also fundamentally incompatible in the underlying
    mechanics of how they work).

    So as far as I am aware, the fix you already found is the only way forward
    as you have posed the problem.

    On 19 September 2014 12:06, wrote:

    If you have the following library:

    type Command interface {
    Run()(i*nterface {}*, error)
    }

    func Execute(command Command)(*interface{},* error){
    return command.Run()
    }

    and the client tries to use it like this:

    type FirstCommand struct {}
    func (_ FirstCommand) Run()(*int*, error) {
    return 0, errors.New("oh oh,... service is returning an error")
    }

    ... the compiler protests that i am not implementing the interface (have (
    *int,error) *but want* (interface{}, error)*

    So my question is: is there any way to do the above i.e. declaring
    arbitrary return types for interfaces and not forcing the clients of my
    library to write code like this?
    func (_ FirstCommand) Run()(*interface{}*, error) { // <--- this would
    fix it but it does not look right to me.
    return 0, errors.New("oh oh,... service is returning an error")
    }


    Thanks in advance

    JTeso







    --
    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.
    --
    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
postedSep 19, '14 at 12:34p
activeSep 19, '14 at 12:56p
posts2
users2
websitegolang.org

2 users in discussion

Jtejob: 1 post Peter Waller: 1 post

People

Translate

site design / logo © 2021 Grokbase