Return bool from Optional[T].Value() instead of an error

This commit is contained in:
Elara 2023-10-04 16:44:07 -07:00
parent 7e2677c495
commit 8a4f704788
1 changed files with 3 additions and 6 deletions

View File

@ -3,13 +3,10 @@ package lemmy
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/url"
)
var ErrOptionalEmpty = errors.New("optional value is empty")
// Optional represents an optional value
type Optional[T any] struct {
value *T
@ -51,11 +48,11 @@ func (o Optional[T]) MustValue() T {
}
// Value returns the value in the optional.
func (o Optional[T]) Value() (T, error) {
func (o Optional[T]) Value() (T, bool) {
if o.value != nil {
return *o.value, ErrOptionalEmpty
return *o.value, true
}
return *new(T), nil
return *new(T), false
}
// ValueOr returns the value inside the optional if it exists, or else it returns fallback