Skip to content

Verify Interface Compliance with Generic support #209

Description

@agtabesh

Using var _ http.Handler = (*Handler)(nil) is a great idea to make sure that Handler implements http.Handler properly.
What if both the interface and the struct using generic?

For example:

type Score interface {
	int64 | float64
}

type ScoreCalculator[T Score] interface {
	Add(score T) T
}

type SampleAlgorithm[T Score] struct {}
func (s *SampleAlgorithm[T]) Add(score T) T {
	// do some manipulation
	return score
}

Should I use both types for verifying compliance?

var _ ScoreCalculator[int64] = (*SampleAlgorithm[int64])(nil)
var _ ScoreCalculator[float64] = (*SampleAlgorithm[float64])(nil)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions