Type func with interface parameter incompatible error

前端 未结 1 706
醉酒成梦
醉酒成梦 2020-12-21 17:35

I have declared a new type func that takes any value that conforms to interface{}. However, when I invoke a function that has been passed as an arg

相关标签:
1条回答
  • 2020-12-21 17:54

    The concept you're looking for here is variance in the type system. Some type systems and types support covariance and contravariance, but Go's interfaces do not.

    While an int can be passed to a function that expects interface{}, the same cannot be said about func(int) and func(interface{}), because interfaces do not behave covariantly.

    If type x implements interface ii, it doesn't mean that func(x) implements func(ii).

    What you could do is pass func(int) into a function that expects interface{}, so you could do

    package main
    
    import "fmt"
    
    func foo(x interface{}) {
        fmt.Println("foo", x)
    }
    
    func add2(n int) int {
        return n + 2
    }
    
    func main() {
        foo(add2)
    }
    

    Because func(int)int does implement interface{}.


    In addition to the Wikipedia link at the top of the answer, this post provides more details about the different kinds of variance programming languages support. It mostly uses other languages, because variance is best demonstrated with languages that support inheritance.

    0 讨论(0)
提交回复
热议问题