Closed fenollp closed 1 year ago
I think it would be great to have more general solution for the options. something like
type Option struct {
capacity int
}
func mergeOptions(opts ...Option) Option {
var res Option
for _, opt := range opts {
if opt.capacity > 0 {
res.capacity = opt.capacity
}
}
return res
}
func WithCapacity(v int) Option {
return Option{capacity: v}
}
func New[K comparable, V any](opts ...Option) *OrderedMap[K, V] {
opt := mergeOptions(opts...)
pairs = make(map[K]*Pair[K, V], opt.capacity)
...
}
this is more flexible solution, the option structure can be easily extended
or using the functions based approach:
type Option func(opt *option)
type option struct {
capacity int
}
func mergeOptions(opts ...Option) option {
res := &option{}
for _, opt := range opts {
opt(res)
}
return res
}
func WithCapacity(v int) Option {
return func(opt *option) {
opt.capacity = v
}
}
func New[K comparable, V any](opts ...Option) *OrderedMap[K, V] {
opt := mergeOptions(opts...)
pairs = make(map[K]*Pair[K, V], opt.capacity)
...
}
@SVilgelm : I agree in general, but in this specific case:
So I'm inclined to keep this as is? Thoughts?
Rebased!
So I'm inclined to keep this as is? Thoughts?
Agreed!
Thanks for the PR @fenollp , might adding a test too?