option

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 16, 2024 License: MIT Imports: 2 Imported by: 0

README

Optional Values in Go

GitHub release (with filter) Actions Status Go Reference Go Report Card codecov

Support user-friendly, type-safe optionals in Go.

value = option.Some(4)
no_value = option.None[int]()

Read the docs.

Usage

This package adds a single type, the Option. Option's are instantiated as one of two variants. Some denotes the presence of a value and None denotes the absence.

Historically pointers have been used to denote optional values, this package removes the risk of null pointer exceptions by leveraging generics to implement type-safe optional values.

Options can be tested for the presence of a value:

two = option.Some(2)
if two.IsSome() {
    ...
}

Values can be extracted along with a boolean test for their presence:

two = option.Some(2)
if value, ok := two.Value(); ok {
    ...
}

Optionals that you're sure have a value can be "unwrapped":

two := option.Some(2)
two.Unwrap()  // returns 2

Accessing a value on a None variant will cause a runtime panic.

none := option.None[int]()
none.Unwrap()  // panics

Ergonomics

Use of a package like this may be pervasive if you really commit to it. This package was inspired by Rust's options implemenation. It might be worth considering dropping the repetative option. preceding the variants. Since importing names into the global namespace is to be avoided, the following import pattern may work for you:

import (
    "fmt"

    "github.com/BooleanCat/option"
)

var (
    Some = option.Some
    None = option.None
)

func main() {
    two := Some(2)
    fmt.Println(two)
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option[T any] struct {
	// contains filtered or unexported fields
}

Option represents an optional value. The Some variant contains a value and the None variant represents the absence of a value.

func None

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

None instantiates an Option with no value.

func Some

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

Some instantiates an Option with a value.

func (Option[T]) Expect

func (o Option[T]) Expect(message string) T

Expect returns the underlying value for a Some variant, or panics with the provided message for a None variant.

Example
package main

import (
	"fmt"

	"github.com/BooleanCat/option"
)

func main() {
	fmt.Println(option.Some(4).Expect("oops"))

}
Output:

4

func (Option[T]) IsNone

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

IsNone returns true if the Option is a None variant.

Example
package main

import (
	"fmt"

	"github.com/BooleanCat/option"
)

func main() {
	fmt.Println(option.Some(4).IsNone())
	fmt.Println(option.None[int]().IsNone())

}
Output:

false
true

func (Option[T]) IsSome

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

IsSome returns true if the Option is a Some variant.

Example
package main

import (
	"fmt"

	"github.com/BooleanCat/option"
)

func main() {
	fmt.Println(option.Some(4).IsSome())
	fmt.Println(option.None[int]().IsSome())

}
Output:

true
false

func (Option[T]) MarshalJSON

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

MarshalJSON implements the json.Marshaler interface.

  • Some variants will be marshaled as their underlying value.
  • None variants will be marshaled as "null".

func (Option[T]) String

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

String implements the fmt.Stringer interface.

func (*Option[T]) UnmarshalJSON

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

UnmarshalJSON implements the json.Unmarshaler interface.

  • Values will be unmarshaled as Some variants.
  • "null"s will be unmarshaled as None variants.

func (Option[T]) Unwrap

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

Unwrap returns the underlying value of a Some variant, or panics if called on a None variant.

Example
package main

import (
	"fmt"

	"github.com/BooleanCat/option"
)

func main() {
	fmt.Println(option.Some(4).Unwrap())
}
Output:

4

func (Option[T]) UnwrapOr

func (o Option[T]) UnwrapOr(value T) T

UnwrapOr returns the underlying value of a Some variant, or the provided value on a None variant.

Example
package main

import (
	"fmt"

	"github.com/BooleanCat/option"
)

func main() {
	fmt.Println(option.Some(4).UnwrapOr(3))
	fmt.Println(option.None[int]().UnwrapOr(3))
}
Output:

4
3

func (Option[T]) UnwrapOrElse

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

UnwrapOrElse returns the underlying value of a Some variant, or the result of calling the provided function on a None variant.

Example
package main

import (
	"fmt"

	"github.com/BooleanCat/option"
)

func main() {
	fmt.Println(option.Some(4).UnwrapOrElse(func() int {
		return 3
	}))

	fmt.Println(option.None[int]().UnwrapOrElse(func() int {
		return 3
	}))

}
Output:

4
3

func (Option[T]) UnwrapOrZero

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

UnwrapOrZero returns the underlying value of a Some variant, or the zero value on a None variant.

Example
package main

import (
	"fmt"

	"github.com/BooleanCat/option"
)

func main() {
	fmt.Println(option.Some(4).UnwrapOrZero())
	fmt.Println(option.None[int]().UnwrapOrZero())

	// Output
	// 4
	// 0
}

func (Option[T]) Value

func (o Option[T]) Value() (T, bool)

Value returns the underlying value and true for a Some variant, or the zero value and false for a None variant.

Example
package main

import (
	"fmt"

	"github.com/BooleanCat/option"
)

func main() {
	value, ok := option.Some(4).Value()
	fmt.Println(value)
	fmt.Println(ok)

}
Output:

4
true

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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