optional

package module
v0.10.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 8, 2022 License: MIT Imports: 4 Imported by: 15

README

go-optional .github/workflows/check.yml codecov GoDoc

A library that provides Go Generics friendly "optional" features.

Synopsis

some := optional.Some[int](123)
fmt.Printf("%v\n", some.IsSome()) // => true
fmt.Printf("%v\n", some.IsNone()) // => false

v, err := some.Take()
fmt.Printf("err is nil: %v\n", err == nil) // => err is nil: true
fmt.Printf("%d\n", v) // => 123

mapped := optional.Map(some, func (v int) int {
    return v * 2
})
fmt.Printf("%v\n", mapped.IsSome()) // => true

mappedValue, _ := some.Take()
fmt.Printf("%d\n", mappedValue) // => 246
none := optional.None[int]()
fmt.Printf("%v\n", none.IsSome()) // => false
fmt.Printf("%v\n", none.IsNone()) // => true

_, err := none.Take()
fmt.Printf("err is nil: %v\n", err == nil) // => err is nil: false
// the error must be `ErrNoneValueTaken`

mapped := optional.Map(none, func (v int) int {
    return v * 2
})
fmt.Printf("%v\n", mapped.IsNone()) // => true

and more detailed examples are here: ./examples_test.go.

Docs

GoDoc

Supported Operations
Value Factory Methods
Option value handler methods
nil == None[T]

This library deals with nil as same as None[T]. So it works with like the following example:

var nilValue Option[int] = nil
fmt.Printf("%v\n", nilValue.IsNone()) // => true
fmt.Printf("%v\n", nilValue.IsSome()) // => false
JSON marshal/unmarshal support

This Option[T] type supports JSON marshal and unmarshal.

If the value wanted to marshal is Some[T] then it marshals that value into the JSON bytes simply, and in unmarshaling, if the given JSON string/bytes has the actual value on corresponded property, it unmarshals that value into Some[T] value.

example:

type JSONStruct struct {
	Val Option[int] `json:"val"`
}

some := Some[int](123)
jsonStruct := &JSONStruct{Val: some}

marshal, err := json.Marshal(jsonStruct)
if err != nil {
	return err
}
fmt.Printf("%s\n", marshal) // => {"val":123}

var unmarshalJSONStruct JSONStruct
err = json.Unmarshal(marshal, &unmarshalJSONStruct)
if err != nil {
	return err
}
// unmarshalJSONStruct.Val == Some[int](123)

Elsewise, when the value is None[T], the marshaller serializes that value as null. And if the unmarshaller gets the JSON null value on a property corresponding to the Optional[T] value, or the value of a property is missing, that deserializes that value as None[T].

example:

type JSONStruct struct {
	Val Option[int] `json:"val"`
}

none := None[int]()
jsonStruct := &JSONStruct{Val: none}

marshal, err := json.Marshal(jsonStruct)
if err != nil {
	return err
}
fmt.Printf("%s\n", marshal) // => {"val":null}

var unmarshalJSONStruct JSONStruct
err = json.Unmarshal(marshal, &unmarshalJSONStruct)
if err != nil {
	return err
}
// unmarshalJSONStruct.Val == None[int]()

And this also supports omitempty option for JSON unmarshaling. If the value of the property is None[T] and that property has omitempty option, it omits that property.

ref:

The "omitempty" option specifies that the field should be omitted from the encoding if the field has an empty value, defined as false, 0, a nil pointer, a nil interface value, and any empty array, slice, map, or string. https://pkg.go.dev/encoding/json#Marshal

example:

type JSONStruct struct {
	OmitemptyVal Option[string] `json:"omitemptyVal,omitempty"` // this should be omitted
}

jsonStruct := &JSONStruct{OmitemptyVal: None[string]()}
marshal, err := json.Marshal(jsonStruct)
if err != nil {
	return err
}
fmt.Printf("%s\n", marshal) // => {}

Known Issues

The runtime raises a compile error like "methods cannot have type parameters", so Map(), MapOr(), MapWithError(), MapOrWithError(), Zip(), ZipWith(), Unzip() and UnzipWith() have been providing as functions. Basically, it would be better to provide them as the methods, but currently, it compromises with the limitation.

Author

moznion (moznion@mail.moznion.net)

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoneValueTaken represents the error that is raised when None value is taken.
	ErrNoneValueTaken = errors.New("none value taken")
)

Functions

func FlatMapOr added in v0.4.0

func FlatMapOr[T, U any](option Option[T], fallbackValue U, mapper func(v T) Option[U]) U

FlatMapOr converts given Option value to another *actual* value according to the mapper function. The difference from the MapOr is the mapper function returns an Option value instead of the bare value. If given Option value is None or mapper function returns None, this returns fallbackValue.

Example
mapper := func(v int) Option[string] {
	return Some[string](fmt.Sprintf("%d", v))
}

some := Some[int](1)
mapped := FlatMapOr(some, "N/A", mapper)
fmt.Printf("%s\n", mapped)

none := None[int]()
mapped = FlatMapOr(none, "N/A", mapper)
fmt.Printf("%s\n", mapped)
Output:

1
N/A

func FlatMapOrWithError added in v0.4.0

func FlatMapOrWithError[T, U any](option Option[T], fallbackValue U, mapper func(v T) (Option[U], error)) (U, error)

FlatMapOrWithError converts given Option value to another *actual* value according to the mapper function that has the ability to return the value with an error. The difference from the MapOrWithError is the mapper function returns an Option value instead of the bare value. If given Option value is None, this returns (fallbackValue, nil). Else if the mapper returns an error then returns ($zero_value_of_type, error). Unless of them, i.e. given Option value is Some and the mapper doesn't return the error, this returns (U, nil).

Example
mapperWithNoError := func(v int) (Option[string], error) {
	return Some[string](fmt.Sprintf("%d", v)), nil
}

some := Some[int](1)
mapped, err := FlatMapOrWithError(some, "N/A", mapperWithNoError)
fmt.Printf("err is nil: %v\n", err == nil)
fmt.Printf("%s\n", mapped)

none := None[int]()
mapped, err = FlatMapOrWithError(none, "N/A", mapperWithNoError)
fmt.Printf("err is nil: %v\n", err == nil)
fmt.Printf("%s\n", mapped)

mapperWithError := func(v int) (Option[string], error) {
	return Some[string]("<ignore-me>"), errors.New("something wrong")
}
mapped, err = FlatMapOrWithError(some, "N/A", mapperWithError)
fmt.Printf("err is nil: %v\n", err == nil)
fmt.Printf("%s\n", mapped)
Output:

err is nil: true
1
err is nil: true
N/A
err is nil: false

func MapOr

func MapOr[T, U any](option Option[T], fallbackValue U, mapper func(v T) U) U

MapOr converts given Option value to another *actual* value according to the mapper function. If given Option value is None, this returns fallbackValue.

Example
mapper := func(v int) string {
	return fmt.Sprintf("%d", v)
}

some := Some[int](1)
mapped := MapOr(some, "N/A", mapper)
fmt.Printf("%s\n", mapped)

none := None[int]()
mapped = MapOr(none, "N/A", mapper)
fmt.Printf("%s\n", mapped)
Output:

1
N/A

func MapOrWithError added in v0.1.0

func MapOrWithError[T, U any](option Option[T], fallbackValue U, mapper func(v T) (U, error)) (U, error)

MapOrWithError converts given Option value to another *actual* value according to the mapper function that has the ability to return the value with an error. If given Option value is None, this returns (fallbackValue, nil). Else if the mapper returns an error then returns (_, error). Unless of them, i.e. given Option value is Some and the mapper doesn't return the error, this returns (U, nil).

Example
mapperWithNoError := func(v int) (string, error) {
	return fmt.Sprintf("%d", v), nil
}

some := Some[int](1)
mapped, err := MapOrWithError(some, "N/A", mapperWithNoError)
fmt.Printf("err is nil: %v\n", err == nil)
fmt.Printf("%s\n", mapped)

none := None[int]()
mapped, err = MapOrWithError(none, "N/A", mapperWithNoError)
fmt.Printf("err is nil: %v\n", err == nil)
fmt.Printf("%s\n", mapped)

mapperWithError := func(v int) (string, error) {
	return "<ignore-me>", errors.New("something wrong")
}
mapped, err = MapOrWithError(some, "N/A", mapperWithError)
fmt.Printf("err is nil: %v\n", err == nil)
fmt.Printf("%s\n", mapped)
Output:

err is nil: true
1
err is nil: true
N/A
err is nil: false
<ignore-me>

func Unzip

func Unzip[T, U any](zipped Option[Pair[T, U]]) (Option[T], Option[U])

Unzip extracts the values from a Pair and pack them into each Option value. If the given zipped value is None, this returns None for all return values.

Example
// see also ExampleZip()

pair := Pair[int, string]{
	Value1: 123,
	Value2: "foo",
}

o1, o2 := Unzip(Some[Pair[int, string]](pair))
fmt.Printf("%d\n", o1.TakeOr(0))
fmt.Printf("%s\n", o2.TakeOr(""))

o1, o2 = Unzip(None[Pair[int, string]]())
fmt.Printf("is none => %v\n", o1.IsNone())
fmt.Printf("is none => %v\n", o2.IsNone())
Output:

123
foo
is none => true
is none => true

func UnzipWith

func UnzipWith[T, U, V any](zipped Option[V], unzipper func(zipped V) (T, U)) (Option[T], Option[U])

UnzipWith extracts the values from the given value according to the unzipper function and pack the into each Option value. If the given zipped value is None, this returns None for all return values.

Example
// see also ExampleZipWith()

type Data struct {
	A int
	B string
}

unzipper := func(d Data) (int, string) {
	return d.A, d.B
}

o1, o2 := UnzipWith(Some[Data](Data{
	A: 123,
	B: "foo",
}), unzipper)
fmt.Printf("%d\n", o1.TakeOr(0))
fmt.Printf("%s\n", o2.TakeOr(""))

o1, o2 = UnzipWith(None[Data](), unzipper)
fmt.Printf("is none => %v\n", o1.IsNone())
fmt.Printf("is none => %v\n", o2.IsNone())
Output:

123
foo
is none => true
is none => true

Types

type Option

type Option[T any] []T

Option is a data type that must be Some (i.e. having a value) or None (i.e. doesn't have a value).

func FlatMap added in v0.4.0

func FlatMap[T, U any](option Option[T], mapper func(v T) Option[U]) Option[U]

FlatMap converts give Option value to another Option value according to the mapper function. The difference from the Map is the mapper function returns an Option value instead of the bare value. If given Option value is None, this also returns None.

Example
mapper := func(v int) Option[string] {
	return Some[string](fmt.Sprintf("%d", v))
}

some := Some[int](1)
opt := FlatMap(some, mapper)
fmt.Printf("%s\n", opt.TakeOr("N/A"))

none := None[int]()
opt = FlatMap(none, mapper)
fmt.Printf("%s\n", opt.TakeOr("N/A"))
Output:

1
N/A

func FlatMapWithError added in v0.4.0

func FlatMapWithError[T, U any](option Option[T], mapper func(v T) (Option[U], error)) (Option[U], error)

FlatMapWithError converts given Option value to another Option value according to the mapper function that has the ability to return the value with an error. The difference from the MapWithError is the mapper function returns an Option value instead of the bare value. If given Option value is None, this returns (None, nil). Else if the mapper returns an error then this returns (None, error). Unless of them, i.e. given Option value is Some and the mapper doesn't return the error, this returns (Some[U], nil).

Example
mapperWithNoError := func(v int) (Option[string], error) {
	return Some[string](fmt.Sprintf("%d", v)), nil
}

some := Some[int](1)
opt, err := FlatMapWithError(some, mapperWithNoError)
fmt.Printf("err is nil: %v\n", err == nil)
fmt.Printf("%s\n", opt.TakeOr("N/A"))

none := None[int]()
opt, err = FlatMapWithError(none, mapperWithNoError)
fmt.Printf("err is nil: %v\n", err == nil)
fmt.Printf("%s\n", opt.TakeOr("N/A"))

mapperWithError := func(v int) (Option[string], error) {
	return Some[string](""), errors.New("something wrong")
}
opt, err = FlatMapWithError(some, mapperWithError)
fmt.Printf("err is nil: %v\n", err == nil)
fmt.Printf("%s\n", opt.TakeOr("N/A"))
Output:

err is nil: true
1
err is nil: true
N/A
err is nil: false
N/A

func FromNillable added in v0.7.0

func FromNillable[T any](v *T) Option[T]

FromNillable is a function to make an Option type value with the nillable value with value de-referencing. If the given value is not nil, this returns Some[T] value. On the other hand, if the value is nil, this returns None[T]. This function does "dereference" for the value on packing that into Option value. If this value is not preferable, please consider using PtrFromNillable() instead.

func Map

func Map[T, U any](option Option[T], mapper func(v T) U) Option[U]

Map converts given Option value to another Option value according to the mapper function. If given Option value is None, this also returns None.

Example
mapper := func(v int) string {
	return fmt.Sprintf("%d", v)
}

some := Some[int](1)
opt := Map(some, mapper)
fmt.Printf("%s\n", opt.TakeOr("N/A"))

none := None[int]()
opt = Map(none, mapper)
fmt.Printf("%s\n", opt.TakeOr("N/A"))
Output:

1
N/A

func MapWithError added in v0.1.0

func MapWithError[T, U any](option Option[T], mapper func(v T) (U, error)) (Option[U], error)

MapWithError converts given Option value to another Option value according to the mapper function that has the ability to return the value with an error. If given Option value is None, this returns (None, nil). Else if the mapper returns an error then this returns (None, error). Unless of them, i.e. given Option value is Some and the mapper doesn't return the error, this returns (Some[U], nil).

Example
mapperWithNoError := func(v int) (string, error) {
	return fmt.Sprintf("%d", v), nil
}

some := Some[int](1)
opt, err := MapWithError(some, mapperWithNoError)
fmt.Printf("err is nil: %v\n", err == nil)
fmt.Printf("%s\n", opt.TakeOr("N/A"))

none := None[int]()
opt, err = MapWithError(none, mapperWithNoError)
fmt.Printf("err is nil: %v\n", err == nil)
fmt.Printf("%s\n", opt.TakeOr("N/A"))

mapperWithError := func(v int) (string, error) {
	return "", errors.New("something wrong")
}
opt, err = MapWithError(some, mapperWithError)
fmt.Printf("err is nil: %v\n", err == nil)
fmt.Printf("%s\n", opt.TakeOr("N/A"))
Output:

err is nil: true
1
err is nil: true
N/A
err is nil: false
N/A

func None

func None[T any]() Option[T]

None is a function to make an Option type value that doesn't have a value.

func PtrFromNillable added in v0.7.0

func PtrFromNillable[T any](v *T) Option[*T]

PtrFromNillable is a function to make an Option type value with the nillable value without value de-referencing. If the given value is not nil, this returns Some[*T] value. On the other hand, if the value is nil, this returns None[*T]. This function doesn't "dereference" the value on packing that into the Option value; in other words, this puts the as-is pointer value into the Option envelope. This behavior contrasts with the FromNillable() function's one.

func Some

func Some[T any](v T) Option[T]

Some is a function to make an Option type value with the actual value.

func Zip

func Zip[T, U any](opt1 Option[T], opt2 Option[U]) Option[Pair[T, U]]

Zip zips two Options into a Pair that has each Option's value. If either one of the Options is None, this also returns None.

Example
maybePair := Zip(Some[int](1), Some[string]("foo"))
pair, err := maybePair.Take()
fmt.Printf("is none => %v\n", maybePair.IsNone())
fmt.Printf("err is nil => %v\n", err == nil)
fmt.Printf("%d %s\n", pair.Value1, pair.Value2)

maybePair = Zip(Some[int](1), None[string]())
fmt.Printf("is none => %v\n", maybePair.IsNone())

maybePair = Zip(None[int](), Some[string]("foo"))
fmt.Printf("is none => %v\n", maybePair.IsNone())

maybePair = Zip(None[int](), None[string]())
fmt.Printf("is none => %v\n", maybePair.IsNone())
Output:

is none => false
err is nil => true
1 foo
is none => true
is none => true
is none => true

func ZipWith

func ZipWith[T, U, V any](opt1 Option[T], opt2 Option[U], zipper func(opt1 T, opt2 U) V) Option[V]

ZipWith zips two Options into a typed value according to the zipper function. If either one of the Options is None, this also returns None.

Example
type Data struct {
	A int
	B string
}

zipper := func(v1 int, v2 string) Data {
	return Data{
		A: v1,
		B: v2,
	}
}

maybeData := ZipWith(Some[int](1), Some[string]("foo"), zipper)
fmt.Printf("is none => %v\n", maybeData.IsNone())
d, err := maybeData.Take()
fmt.Printf("err is nil => %v\n", err == nil)
fmt.Printf("%d %s\n", d.A, d.B)

maybeData = ZipWith(Some[int](1), None[string](), zipper)
fmt.Printf("is none => %v\n", maybeData.IsNone())
maybeData = ZipWith(None[int](), Some[string]("foo"), zipper)
fmt.Printf("is none => %v\n", maybeData.IsNone())
maybeData = ZipWith(None[int](), None[string](), zipper)
fmt.Printf("is none => %v\n", maybeData.IsNone())
Output:

is none => false
err is nil => true
1 foo
is none => true
is none => true
is none => true

func (Option[T]) Filter

func (o Option[T]) Filter(predicate func(v T) bool) Option[T]

Filter returns self if the Option has a value and the value matches the condition of the predicate function. In other cases (i.e. it doesn't match with the predicate or the Option is None), this returns None value.

Example
isEven := func(v int) bool {
	return v%2 == 0
}

some := Some[int](2)
opt := some.Filter(isEven)
fmt.Printf("%d\n", opt.TakeOr(0))

some = Some[int](1)
opt = some.Filter(isEven)
fmt.Printf("%d\n", opt.TakeOr(0))

none := None[int]()
opt = none.Filter(isEven)
fmt.Printf("%d\n", opt.TakeOr(0))
Output:

2
0
0

func (Option[T]) IfNone added in v0.2.0

func (o Option[T]) IfNone(f func())

IfNone calls given function if the receiver value is None.

Example
None[string]().IfNone(func() {
	fmt.Println("value is none")
})

Some("foo").IfNone(func() {
	fmt.Println("do not show this message")
})
Output:

value is none

func (Option[T]) IfNoneWithError added in v0.2.0

func (o Option[T]) IfNoneWithError(f func() error) error

IfNoneWithError calls given function if the receiver value is None. This method propagates the error of given function, and if the receiver value is Some, this returns nil error.

Example
err := None[string]().IfNoneWithError(func() error {
	fmt.Println("value is none")
	return nil
})
if err != nil {
	fmt.Println(err) // no error
}

err = None[string]().IfNoneWithError(func() error {
	fmt.Println("value is none!!")
	return errors.New("^^^ error occurred")
})
if err != nil {
	fmt.Println(err)
}

err = Some("foo").IfNoneWithError(func() error {
	return errors.New("do not show this error")
})
if err != nil {
	fmt.Println(err) // must not show this error
}
Output:

value is none
value is none!!
^^^ error occurred

func (Option[T]) IfSome added in v0.2.0

func (o Option[T]) IfSome(f func(v T))

IfSome calls given function with the value of Option if the receiver value is Some.

Example
Some("foo").IfSome(func(val string) {
	fmt.Println(val)
})

None[string]().IfSome(func(val string) {
	fmt.Println("do not show this message")
})
Output:

foo

func (Option[T]) IfSomeWithError added in v0.2.0

func (o Option[T]) IfSomeWithError(f func(v T) error) error

IfSomeWithError calls given function with the value of Option if the receiver value is Some. This method propagates the error of given function, and if the receiver value is None, this returns nil error.

Example
err := Some("foo").IfSomeWithError(func(val string) error {
	fmt.Println(val)
	return nil
})
if err != nil {
	fmt.Println(err) // no error
}

err = Some("bar").IfSomeWithError(func(val string) error {
	fmt.Println(val)
	return errors.New("^^^ error occurred")
})
if err != nil {
	fmt.Println(err)
}

err = None[string]().IfSomeWithError(func(val string) error {
	return errors.New("do not show this error")
})
if err != nil {
	fmt.Println(err) // must not show this error
}
Output:

foo
bar
^^^ error occurred

func (Option[T]) IsNone

func (o Option[T]) IsNone() bool

IsNone returns whether the Option *doesn't* have a value or not.

Example
some := Some[int](1)
fmt.Printf("%v\n", some.IsNone())
none := None[int]()
fmt.Printf("%v\n", none.IsNone())

num := 123
some = FromNillable[int](&num)
fmt.Printf("%v\n", some.IsNone())
none = FromNillable[int](nil)
fmt.Printf("%v\n", none.IsNone())

ptrSome := PtrFromNillable[int](&num)
fmt.Printf("%v\n", ptrSome.IsNone())
ptrNone := PtrFromNillable[int](nil)
fmt.Printf("%v\n", ptrNone.IsNone())

var nilValue Option[int] = nil
fmt.Printf("%v\n", nilValue.IsNone())
Output:

false
true
false
true
false
true
true

func (Option[T]) IsSome

func (o Option[T]) IsSome() bool

IsSome returns whether the Option has a value or not.

Example
some := Some[int](1)
fmt.Printf("%v\n", some.IsSome())
none := None[int]()
fmt.Printf("%v\n", none.IsSome())

num := 123
some = FromNillable[int](&num)
fmt.Printf("%v\n", some.IsSome())
none = FromNillable[int](nil)
fmt.Printf("%v\n", none.IsSome())

ptrSome := PtrFromNillable[int](&num)
fmt.Printf("%v\n", ptrSome.IsSome())
ptrNone := PtrFromNillable[int](nil)
fmt.Printf("%v\n", ptrNone.IsSome())

var nilValue Option[int] = nil
fmt.Printf("%v\n", nilValue.IsSome())
Output:

true
false
true
false
true
false
false

func (Option[T]) MarshalJSON added in v0.5.0

func (o Option[T]) MarshalJSON() ([]byte, error)

func (Option[T]) Or added in v0.10.0

func (o Option[T]) Or(fallbackOptionValue Option[T]) Option[T]

Or returns the Option value according to the actual value existence. If the receiver's Option value is Some, this function pass-through that to return. Otherwise, this value returns the `fallbackOptionValue`.

Example
fallback := Some[string]("fallback")

some := Some[string]("actual")
fmt.Printf("%s\n", some.Or(fallback))

none := None[string]()
fmt.Printf("%s\n", none.Or(fallback))
Output:

Some[actual]
Some[fallback]

func (Option[T]) OrElse added in v0.9.0

func (o Option[T]) OrElse(fallbackOptionFunc func() Option[T]) Option[T]

OrElse returns the Option value according to the actual value existence. If the receiver's Option value is Some, this function pass-through that to return. Otherwise, this executes `fallbackOptionFunc` and returns the result value of that function.

Example
fallbackFunc := func() Option[string] { return Some[string]("fallback") }

some := Some[string]("actual")
fmt.Printf("%s\n", some.OrElse(fallbackFunc))

none := None[string]()
fmt.Printf("%s\n", none.OrElse(fallbackFunc))
Output:

Some[actual]
Some[fallback]

func (Option[T]) String added in v0.8.0

func (o Option[T]) String() string

func (Option[T]) Take

func (o Option[T]) Take() (T, error)

Take takes the contained value in Option. If Option value is Some, this returns the value that is contained in Option. On the other hand, this returns an ErrNoneValueTaken as the second return value.

Example
some := Some[int](1)
v, err := some.Take()
fmt.Printf("%d\n", v)
fmt.Printf("%v\n", err == nil)

none := None[int]()
_, err = none.Take()
fmt.Printf("%v\n", err == nil)
Output:

1
true
false

func (Option[T]) TakeOr

func (o Option[T]) TakeOr(fallbackValue T) T

TakeOr returns the actual value if the Option has a value. On the other hand, this returns fallbackValue.

Example
some := Some[int](1)
v := some.TakeOr(666)
fmt.Printf("%d\n", v)

none := None[int]()
v = none.TakeOr(666)
fmt.Printf("%d\n", v)
Output:

1
666

func (Option[T]) TakeOrElse

func (o Option[T]) TakeOrElse(fallbackFunc func() T) T

TakeOrElse returns the actual value if the Option has a value. On the other hand, this executes fallbackFunc and returns the result value of that function.

Example
some := Some[int](1)
v := some.TakeOrElse(func() int {
	return 666
})
fmt.Printf("%d\n", v)

none := None[int]()
v = none.TakeOrElse(func() int {
	return 666
})
fmt.Printf("%d\n", v)
Output:

1
666

func (*Option[T]) UnmarshalJSON added in v0.5.0

func (o *Option[T]) UnmarshalJSON(data []byte) error

func (Option[T]) Unwrap added in v0.3.0

func (o Option[T]) Unwrap() T

Unwrap returns the value regardless of Some/None status. If the Option value is Some, this method returns the actual value. On the other hand, if the Option value is None, this method returns the *default* value according to the type.

Example
fmt.Printf("%v\n", Some[int](12345).Unwrap())
fmt.Printf("%v\n", None[int]().Unwrap())
fmt.Printf("%v\n", None[*int]().Unwrap())

num := 123
fmt.Printf("%v\n", FromNillable[int](&num).Unwrap())
fmt.Printf("%v\n", FromNillable[int](nil).Unwrap())
fmt.Printf("%v\n", *PtrFromNillable[int](&num).Unwrap()) // NOTE: this dereferences tha unwrapped value
fmt.Printf("%v\n", PtrFromNillable[int](nil).Unwrap())
Output:

12345
0
<nil>
123
0
123
<nil>

type Pair

type Pair[T, U any] struct {
	Value1 T
	Value2 U
}

Pair is a data type that represents a tuple that has two elements.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL