mirror of
https://github.com/aykhans/dodo.git
synced 2025-04-20 11:11:26 +00:00
📚 Add docs
This commit is contained in:
parent
adc5a34891
commit
ede74c17b2
@ -5,15 +5,19 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Don't call this struct directly, use NewOption[T] or NewNoneOption[T] instead.
|
||||||
type Option[T any] struct {
|
type Option[T any] struct {
|
||||||
|
// value holds the actual value of the Option if it is not None.
|
||||||
value T
|
value T
|
||||||
none bool
|
// none indicates whether the Option is None (i.e., has no value).
|
||||||
|
none bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *Option[T]) IsNone() bool {
|
func (o *Option[T]) IsNone() bool {
|
||||||
return o.none
|
return o.none
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The returned value can be nil, if the Option is None, it will return nil and an error.
|
||||||
func (o *Option[T]) ValueOrErr() (*T, error) {
|
func (o *Option[T]) ValueOrErr() (*T, error) {
|
||||||
if o.IsNone() {
|
if o.IsNone() {
|
||||||
return nil, errors.New("Option is None")
|
return nil, errors.New("Option is None")
|
||||||
@ -21,6 +25,7 @@ func (o *Option[T]) ValueOrErr() (*T, error) {
|
|||||||
return &o.value, nil
|
return &o.value, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The returned value can't be nil, if the Option is None, it will return the default value.
|
||||||
func (o *Option[T]) ValueOr(def *T) *T {
|
func (o *Option[T]) ValueOr(def *T) *T {
|
||||||
if o.IsNone() {
|
if o.IsNone() {
|
||||||
return def
|
return def
|
||||||
@ -28,6 +33,7 @@ func (o *Option[T]) ValueOr(def *T) *T {
|
|||||||
return &o.value
|
return &o.value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The returned value can't be nil, if the Option is None, it will panic.
|
||||||
func (o *Option[T]) ValueOrPanic() *T {
|
func (o *Option[T]) ValueOrPanic() *T {
|
||||||
if o.IsNone() {
|
if o.IsNone() {
|
||||||
panic("Option is None")
|
panic("Option is None")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user