Documentation
¶
Overview ¶
Package maybe provides a generic, type-safe container for values that may or may not be present.
Maybe[T] mirrors the “Maybe”/“Optional” pattern found in other languages, offering a safer alternative to pointers when you need to represent “some value” vs. “no value.” It supports:
- Construction via Some(v) and None[T]().
- Querying presence with Some() and None() methods.
- Unwrapping with Unwrap(), which panics on None.
- JSON marshalling: encodes None as null, Some(v) as v.
- TOML marshalling: encodes None as the special TomlNone hack.
- Text unmarshalling: treats empty, “null”, or TomlNone (case-insensitive) as None, otherwise attempts to parse into T (using encoding.TextUnmarshaler if available, or a JSON fallback for scalars).
Common helpers include True(), False(), and NoneBool() for boolean Optionals.
Usage:
import "github.com/amberpixels/k1/maybe"
opt := maybe.Some(42)
if opt.Some() {
fmt.Println("We have:", opt.Unwrap())
}
Index ¶
- Constants
- type Bool
- type Int
- type Option
- func (o *Option[T]) IsZero() bool
- func (o Option[T]) MarshalJSON() ([]byte, error)
- func (o Option[T]) MarshalTOML() ([]byte, error)
- func (o *Option[T]) None() bool
- func (o *Option[T]) Some(args ...T) bool
- func (o *Option[T]) UnmarshalJSON(data []byte) error
- func (o *Option[T]) UnmarshalText(text []byte) error
- func (o *Option[T]) Unwrap() T
Constants ¶
const TomlNone = "None"
TomlNone constant for a "None" value. Because TOML doesn't have a null, we're doing it via such a hack.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Option ¶
type Option[T comparable] struct { // contains filtered or unexported fields }
Option is a safe alternative to a pointer to a value of type T. It represents a value that can either be present (Some) or absent (None). For example, for T = bool, it models an optional boolean value that can be true, false, or not specified.
func None ¶
func None[T comparable]() Option[T]
None constructs an Option that does not contain a valid value.
func Some ¶
func Some[T comparable](v T) Option[T]
Some constructs an Option that contains a valid value.
func (Option[T]) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface. If the Option is None, it marshals to JSON null; otherwise, it marshals to the contained value.
func (Option[T]) MarshalTOML ¶
MarshalTOML returns the underlying value if it exists, or nil otherwise.
func (*Option[T]) Some ¶
Some works in two ways:
- When called without arguments, it returns true if the Option contains a valid value.
- When called with one argument, it returns true if the Option contains a valid value and that value is equal to the provided argument.
Panics if more than one argument is provided.
func (*Option[T]) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface. If the JSON value is null, the Option is set to None; otherwise, it unmarshals into the contained value.
func (*Option[T]) UnmarshalText ¶
UnmarshalText implements the encoding.TextUnmarshaler interface. It interprets empty strings or "null" (case-insensitive) as a None value. Otherwise, it attempts to convert the text into type T.