package main
import "fmt"
type Z interface {
A()
}
type Y interface {
Z
B()
}
type A struct{}
func (a A) A() {}
func (a A) B() {}
type Container[T Z] struct {
Value T
}
func main() {
instance := A{} // satisfies both Y and Z
var container Container[Z] = Container[Z]{Value: instance}
// this is not allowed because container is not an interface value
// if _, ok := container.(Container[Y]); ok {
// fmt.Println("it does")
// }
// this is also not possible
// var c2 Container[Y] = container
_ = container
fmt.Println("no luck")
}
Having a generic container which takes any type parameter which satisfies Z, and where interface Y is a specialisation of interface Z. Is there any way to convert a Container[Z] into Container[Y] having the Value of the instance of Container[Z] actually satisfies interface Y?
I know I can make a new instance of Container[Y] and copy the values from the Container[Z] instance into it, but this seems so costly…