Home > front end >  How to use interfaces implemented through embedding in generics
How to use interfaces implemented through embedding in generics

Time:06-26

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.

CodePudding user response:

Foo1 doesn't implement FooInterface. *Foo1 does. That is why you pass &foo1 and not foo1. For the type parameter, you should put *Foo1.

CodePudding user response:

I assume your TakesFooInterface is like this:

func TakesFooInterface(foo1 FooInterface) {
   ...
}

Then the call TakesFooInterface(foo1) failed to compile, as foo1 does not implement FooInterface but &foo1.

The same with your generic case, the Foo1 type does not implement FooInterface which requires the pointer access in the method HasName but *Foo1 type does.

I have copied your code with some of my tryings on Go.Dev: https://go.dev/play/p/3I2mtabi-ub

  • Related