go-conv icon indicating copy to clipboard operation
go-conv copied to clipboard

Can add XXXWithDefault(v, defaultValue XXX) XXX

Open mei-rune opened this issue 3 years ago • 1 comments

Can add XXXWithDefault(v, defaultValue XXX) XXX

func XXXWithDefault(v, defaultValue XXX) XXX { a, err := XXX(v) if err == nil { return a } return defaultValue }

mei-rune avatar Oct 03 '22 01:10 mei-rune

I would be open to adding a function similar to Infer but with support for default values. I don't think I would want to increase the API to support typed variants of everything. In fact with generics released this library would look very different if I was to write it again today.

Some possible API's I may consider pull request for:

// All in one function that behaves like Infer but adds a default value.
func F(dst, v, defaultValue interface{})

// Where F can be one of the below, or some other name that makes sense:
InferWithDefault
InferDefault
InferWith
WithDefault
Default

cstockton avatar Dec 01 '22 18:12 cstockton