【发布时间】:2022-06-10 16:04:20
【问题描述】:
I'm trying to implement a variation of the pointer-method example for type-parameters design pattern, in order to abstract over some unified repository interface.
I was under the impression that the Person struct would inherit the method-set of Entity if it composes *Entity, but I get a compile-time error as below. Could someone please explain why the type-constraint is not met and how to fix this code?
Apologies for the awful title; if someone could suggest an improved summary that would be fantastic (I'm quite new to Go).
Thanks :)
package main
// domain
type PEntity[E any] interface {
*E
SetID(id string)
}
type Entity struct {
ID string
}
func (e Entity) SetID(id string) {
e.ID = id
}
type Repository[E Entity, PE PEntity[E]] interface {
Get(id string) *E
}
// data
type Person struct {
*Entity
}
type PersonRepository interface {
Repository[Person, *Person] // -> Person does not implement Entity
AddPet(name string)
// ...
}
【问题讨论】: