Skip to content

Proposal: Optional results #145

@ngehrsitz

Description

@ngehrsitz

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?

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