Original post

I’m having a bit of a hard time with ’s first class functions. Consider the following:

package main
import "log"
type A interface {}
type B interface {
    A
    foo()
}

func NewA() A { return &AImpl{} }
func NewB() B { return &BImpl{} }

type AImpl struct {}
type BImpl struct {}

func (b *BImpl) foo() {}

func main() {
    var ACtor func() A
    ACtor = NewA // Works, as expected
    ACtor = NewB // Fails, even though B implements A
    log.Println(ACtor) // Throwaway line to mark ACtor as used
}

Why can’t I use `NewB()` as `NewA()`? `B` satisfies `A`, so anywhere I would *directly* call `NewA()` I could instead call `NewB()`, so my intuition is that this should also extend to first class function return types. I’ve managed to get around this by writing a wrapper function:

func NewBasA(ctor func() B) func() A {
    return func() A {
        return ctor()
    }
}

However, this seems a bit janky to me and smells of something the compiler should be auto-generating instead of manually having to write it out. Is there some pattern I’m missing here?