package gonads import "errors" var ( ErrOptionIsNone = errors.New("gonads: Option[T] has no value") ) // Option is a container that might contain a value. type Option[T any] struct { val *T } // IsSome returns true if the Option has some value in it. func (o Option[T]) IsSome() bool { return o.val != nil } // IsNone returns true if the Option has no value in it. func (o Option[T]) IsNone() bool { return !o.IsSome() } // Take a value out of the option if it exists. func (o Option[T]) Take() (*T, error) { if o.IsNone() { return nil, ErrOptionIsNone } return o.val, nil } // Set the value of the option to a given value. func (o *Option[T]) Set(val T) { o.val = &val } // Clear removes the value from an Option. func (o *Option[T]) Clear() { o.val = nil } // NewOption creates a new Option with the given type. All Options // Start out as nil. func NewOption[T any]() *Option[T] { return &Option[T]{} }