4 ms·
For a function, it makes sense to just write two functions, the easiest to use, and the one with maximum flaxibility. func SortOrdered[T constraints.Ordere
by lalaithion 4y ago
For a function, it makes sense to just write two functions, the easiest to use, and the one with maximum flaxibility.
func SortOrdered[T constraints.Ordered](s []T) {
// …
}
func Sort[T any](s []T, less func(T, T) bool) {
// …
}
For a data structure, I recommend using
type SearchTree[T any] struct {
Less func(T, T) bool
// …
}
and then writing two constructors
func NewSearchTree(less func(T, T) bool) *SearchTree[T] {
// …
return &SearchTree[T]{
Less: less,
// …
}
}
func NewOrderedSearchTree[T constraints.Ordered]() *SearchTree[T] {
// …
return &SearchTree[T]{
Less: func(x, y T) bool {return x < y},
// …
}
}
- Merovius 4y agoPersonally, I very much dislike that this makes the zero value of `SearchTree[T]` not useful. I personally feel that anything which only handles data (as opposed to something like an os.File) shouldn't require explicit initialization. I genuinely think I might personally converge on the `Comparator[T]` approach only (at least for types), for that reason.