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 argument (conforming to that type specification) I get an error.
Can somebody explain why this is the case? Below is the simplest example I could recreate the issue with.
type myfunc func(x interface{})
func a(num int) {
return
}
func b(f myfunc) {
f(2)
return
}
func main() {
b(a) // error: cannot use a (type func(int)) as type myfunc in argument to b
return
}
Best Answer
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 expectsinterface{}
, the same cannot be said aboutfunc(int)
andfunc(interface{})
, because interfaces do not behave covariantly.If type
x
implements interfaceii
, it doesn't mean thatfunc(x)
implementsfunc(ii)
.What you could do is pass
func(int)
into a function that expectsinterface{}
, so you could doBecause
func(int)int
does implementinterface{}
.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.