maybe

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2025 License: MIT Imports: 4 Imported by: 0

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

View Source
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 Bool

type Bool = Option[bool]

Bool is just a shortcut for Option[bool].

type Int

type Int = Option[int]

Int is just a shortcut for Option[int].

func NoneInt

func NoneInt() Int

NoneInt is a shortcut for None[int].

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 False

func False() Option[bool]

False is a shorthand for Some(false).

func None

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

None constructs an Option that does not contain a valid value.

func NoneBool

func NoneBool() Option[bool]

NoneBool is a shorthand for None[bool]().

func Some

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

Some constructs an Option that contains a valid value.

func True

func True() Option[bool]

True is a shorthand for Some(true).

func (*Option[T]) IsZero

func (o *Option[T]) IsZero() bool

IsZero returns true if Option is none (for `omitzero` interface).

func (Option[T]) MarshalJSON

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

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

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

MarshalTOML returns the underlying value if it exists, or nil otherwise.

func (*Option[T]) None

func (o *Option[T]) None() bool

None returns true if the Option does not contain a valid value.

func (*Option[T]) Some

func (o *Option[T]) Some(args ...T) bool

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

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

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

func (o *Option[T]) UnmarshalText(text []byte) error

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.

func (*Option[T]) Unwrap

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

Unwrap returns the contained value if present; otherwise, it panics. This mirrors Rust's `unwrap`, providing a quick way to extract the value when you are certain that it is present.

Jump to

Keyboard shortcuts

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