Why Go doesn't automatically convert between:
package main
import "fmt"
type Any interface{} // Any is an empty interface
type X func(x Any) // X is a function that receives Any
func Y(x X) { // Y is a function that receives X
x(1)
}
func test(v interface{}) { // test is not considered equal to X
fmt.Println("called",v)
}
func main() {
Y(test) // error: cannot use test (type func(interface {})) as type X in argument to Y
}
Also this one:
package main
import "fmt"
type Any interface{}
func X2(a Any) {
X(a)
}
func Y2(a interface{}) {
X2(a) // this is OK
}
func X(a ...Any) {
fmt.Println(a)
}
func Y(a ...interface{}) { // but this one not ok
X(a...) // error: cannot use a (type []interface {}) as type []Any in argument to X
}
func main() {
v := []int{1,2,3}
X(v)
Y(v)
}
I was really hopeful that interface{}
could be renamed to Any
on anything (slices
, map
, func
) not just simple types
Second question would be: is there a way to make it possible?