I have a function that looks a bit like
func (c *Context) RegisterHandler(f interface{}) error {
// Do lots of checking to make sure a function was passed in
// and it's arguments all implement a specific interface
}
func consumer(a ConcreteTypeA, b ConcreteTypeB) {}
func main() {
...
c.RegisterHandler(consumer)
}
which roughly boils down to
var a ConcreteTypeA
var b ConcreteTypeB
a.InitFromContext(c)
b.InitFromContext(c)
consumer(a, b)
Is there a way I can make that interface{}
more specific without changing it’s behavior? If not I’m fairly happy with my solution. I just hope I can cut down on runtime checks and possible bugs being uncaught at compile time.
My current implementation looks pretty similar to above. I have tried to do something like
func (c *Context) RegisterHandler(f func (...InitFromContexter)) error {
// Do lots of checking to make sure a function was passed in
// and it's arguments all implement a specific interface
}
But that causes all the existing code to fail because it will only receive variadic functions