Search code examples
gogenericsconstraintsembedding

How to satisfy a constraint implemented through embedding in generics?


I have an interface

type FooInterface interface {
    HasName() bool
}

It has a base implementation that I intend to use in other implementation of FooInterface

type FooBoilerplate struct {
    hasName bool
}

func (f *FooBoilerplate) HasName() bool {
    return f.hasName
}

Like so

type Foo1 struct {
    fooId int
    FooBoilerplate
}

func (f *Foo1) GetId() int {
    return f.fooId
}

I want to create a container class for FooInterface using generics

type FooContainer[T FooInterface] struct {
    list []T
}

func (fc *FooContainer[T]) AddItem() {
    var t T

    fc.list = append(fc.list, t)

}

However I get compilation errors when I try to instantiate the container

func RunThis() {
    foo1 := Foo1{FooBoilerplate: FooBoilerplate{hasName: false}}

    // works fine
    TakesFooInterface(&foo1)

    // doesnt compile
    fc := FooContainer[Foo1]{}
    fc.AddItem()
}

Not sure why Foo1 is seen to implement FooInterface when passed to a function, but isnt when passed tot he container struct. I cant pass an object to the container instantiation either.


Solution

  • Foo1 doesn't implement FooInterface. *Foo1 does. This is true irrespective of whether the interface is used as the type of a function argument or as a constraint.

    That is why you pass &foo1 (type *Foo1) to TakesFooInterface and not foo1 (type Foo1).

    Likewise for the type parameter, you should instantiate the generic type with *Foo1:

    fc := FooContainer[*Foo1]{}