I am experimenting with go generics, and I have encountered this issue. Why is Schedule
not matching any
?
Code
func demo(m Model[any]) Model[any] {
return m
}
type Model[M any] interface {
FindOne(db *gorm.DB, conds ...any) (m M)
}
type DbOperations[M any] struct{}
func (d DbOperations[M]) FindOne(db *gorm.DB, conds ...any) (m M) {
res := db.First(&m, conds)
if res.Error != nil {
return m
}
return m
}
// the program does not compile because of this function
func a() {
m := DbOperations[Schedule]{}
get(m) // <= error occurs here
}
Error message
../models/operations.go:103:6: cannot use m (variable of type DbOperations[Schedule]) as type Model[any] in argument to get:
DbOperations[Schedule] does not implement Model[any] (wrong type for FindOne method)
have FindOne(db *gorm.DB, conds ...any) (m Schedule)
want FindOne(db *gorm.DB, conds ...any) any