Proposal: Optional results
Author: ngehrsitzCreated Nov 19, 2024Updated Dec 3, 2024
There are tasks where not having a result is not the error case. For these cases I propose extending the set of Pools with
type OptionalResultPool[T any] struct {}
func (p *OptionalResultPool[T]) Go(f func() *T) {}
func (p *OptionalResultPool[T]) Wait() []T {}
type OptionalResultErrorPool[T any] struct {
func (p *OptionalResultErrorPool[T]) Go(f func() (*T, error)) {}
func (p *OptionalResultErrorPool[T]) Wait() ([]T, error) {}
type OptionalResultContextPool[T any] struct {}
func (p *OptionalResultContextPool[T]) Go(f func(context.Context) (*T, error)) {}
func (p *OptionalResultContextPool[T]) Wait() ([]T, error) {}Wait() would then discard all nil results without an error and dereference the pointers.
The alternative to this that I am currently using is a wrapper type, that I then filter and map to the original type, but a native solution would be preferable.
Should I open a PR for this?
Source: sourcegraph/conc