I currently have an interface X with methods that return itself.
type X interface {
RetSelf(arg string) X
uselessFunc()
uselessFunc2()
usefulFunc()
}
I want to break this out into 2 minimal subsets
type A interface {
RetSelf(arg string) B
}
type B interface {
usefulFunc()
}
It seems to me like structs which fulfil interface X should also fulfill interface B, and therefore interface A. I suspect that I can just change the definition of legacy structs that use X, to use A instead of X, while keeping the same underlying objects.
type user struct {
problemInterface X
}
func NewUser(problemInterface X) {
return user{X}
}
becomes
type user struct {
problemInterface X
}
func NewUser(problemInterface X) {
return user{X}
}
With the actual invocations and underlying structs not changing otherwise.
However, instead of trying this I am wondering if there is some elegant solution to typecast interface X into interface A. I can’t do it trivially because A.RetSelf returns B, which does not have all the methods of X so A.RetSelf != X.RetSelf Is there something I’m missing here or am I just SoL?