mercredi 17 mai 2023

Problem with "define interface where it is used" principle in Golang

I am confused how to follow this rule in cases like this one. For example, I have some package with Criteria which use some kind of visitor

type Visitor interface {
    VisitID(int64)
    VisitLocation(lat, lon float64)
}

type CriteriaIdentity int64

func (c CriteriaIdentity) Apply(v Visitor) {
    v.VisitID(int64(c))
}

type CriteriaLocation struct {
   lat float64
   lon float64
}

func (c *CriteriaIdentity) Apply(v Visitor) {
    v.VisitLocation(c.lat, c.lon)
}

Then, I want to define a criteria interface in some package search:

type SearchCriteria interface {
     Apply(???) // What can I hint here?
}

type User struct {
    engine searchEngine
}

func (u *User) Search(ctx context.Context, criterias ...SearchCriteria) ([]int64, error) {
}

And use my searcher in some other package landing:

type Searcher interface {
    Search(ctx context.Context, criterias ...???) // Same question
}

How can I define hinting for SearchCriteria and `Se in a way that does not violate the rule?

Aucun commentaire:

Enregistrer un commentaire