types

package
v1.7.0 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2025 License: MIT Imports: 2 Imported by: 4

Documentation

Overview

Package types defines a set of useful constraints to be used with type parameters.

Package types provides a collection of useful generic types and constraints for Go applications, enhancing type safety and enabling more expressive generic programming.

This package includes generic constraint types that define sets of types usable with type parameters, such as numeric types, ordered types, or comparable types.

It also includes utility types and structs like tuples or pairs, which simplify working with grouped data.

The `types` package is designed to complement Go's type parameter features, making it easier to write reusable and type-safe code.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Comparable

type Comparable = comparable

Comparable is an interface that is implemented by all comparable types (booleans, numbers, strings, pointers, channels, arrays of comparable types, structs whose fields are all comparable types). The comparable interface may only be used as a type parameter constraint, not as the type of a variable.

type Complex

type Complex interface {
	~complex64 | ~complex128
}

Complex is a constraint that permits any complex numeric type. If future releases of Go add new predeclared complex numeric types, this constraint will be modified to include them.

type Float

type Float interface {
	~float32 | ~float64
}

Float is a constraint that permits any floating-point type. If future releases of Go add new predeclared floating-point types, this constraint will be modified to include them.

type Integer

type Integer interface {
	Signed | Unsigned
}

Integer is a constraint that permits any integer type. If future releases of Go add new predeclared integer types, this constraint will be modified to include them.

type Number

type Number interface {
	Integer | Float
}

Number is a constraint that permits any numeric type. If future releases of Go add new predeclared numeric types, this constraint will be modified to include them.

type Ordered

type Ordered = cmp.Ordered

Ordered is a constraint that permits any ordered type: any type that supports the operators < <= >= >. If future releases of Go add new ordered types, this constraint will be modified to include them.

Note that floating-point types may contain NaN ("not-a-number") values. An operator such as == or < will always report false when comparing a NaN value with any other value, NaN or not. See the [Compare] function for a consistent way to compare NaN values.

type Pair

type Pair[L, R any] struct {
	Left  L
	Right R
}

Pair is a generic type that represents a pair of values.

func NewPair

func NewPair[L, R any](left L, right R) *Pair[L, R]

NewPair creates a new Pair with the given left and right values.

func (*Pair[L, R]) GetLeft

func (p *Pair[L, R]) GetLeft() L

GetLeft returns the left value of the pair.

func (*Pair[L, R]) GetRight

func (p *Pair[L, R]) GetRight() R

GetRight returns the right value of the pair.

func (*Pair[L, R]) Seq

func (p *Pair[L, R]) Seq() iter.Seq[Pair[L, R]]

Seq returns an iter.Seq with this Pair.

This is useful for reusing functions provided by package seq.

func (*Pair[L, R]) Seq2

func (p *Pair[L, R]) Seq2() iter.Seq2[L, R]

Seq2 returns an iter.Seq2 with left and right value.

This is useful for reusing functions provided by package seq2.

func (*Pair[L, R]) ToTuple

func (p *Pair[L, R]) ToTuple() Tuple2[L, R]

ToTuple converts the Pair to a Tuple.

func (*Pair[L, R]) Unpack

func (p *Pair[L, R]) Unpack() (L, R)

Unpack returns the left and right values of the pair.

type PairLike

type PairLike[L, R any] interface {
	GetLeft() L
	GetRight() R
}

PairLike represents any type that is pair-like, it is used for creating a Result instance.

type Result added in v0.24.0

type Result[V any] struct {
	// contains filtered or unexported fields
}

Result is a type representing a result that could be either a value or an error.

func FailureResult added in v0.24.0

func FailureResult[V any](err error) *Result[V]

FailureResult creates a new Result instance with the provided error.

func ResultFrom added in v0.24.0

func ResultFrom[V any](provider func() (V, error)) *Result[V]

ResultFrom creates a Result instance from a function that returns a value and an error.

func ResultFromPair added in v0.24.0

func ResultFromPair[V any](pair PairLike[V, error]) *Result[V]

ResultFromPair creates a Result instance from a PairLike argument.

func ResultOf added in v0.24.0

func ResultOf[V any](value V, err error) *Result[V]

ResultOf creates a new Result instance with the provided value and error.

func SuccessResult added in v0.24.0

func SuccessResult[V any](value V) *Result[V]

SuccessResult creates a new Result instance with the provided value.

func (*Result[V]) Get added in v0.26.0

func (m *Result[V]) Get() (V, error)

Get returns the value and error from the Result instance.

func (*Result[V]) GetError added in v0.26.0

func (m *Result[V]) GetError() error

GetError returns the error from the Result instance. If there is no error, it returns nil.

func (*Result[V]) IsError added in v0.24.0

func (m *Result[V]) IsError() bool

IsError checks if the Result instance contains an error.

func (*Result[V]) IsNotError added in v0.24.0

func (m *Result[V]) IsNotError() bool

IsNotError checks if the Result instance does not contain an error.

func (*Result[V]) MustGetError added in v0.26.0

func (m *Result[V]) MustGetError() error

MustGetError returns the error from the Result instance, panicking if there is no error.

func (*Result[V]) MustGetValue added in v0.24.0

func (m *Result[V]) MustGetValue() V

MustGetValue returns the value from the Result instance, panicking if there is an error.

func (*Result[V]) Or added in v0.24.0

func (m *Result[V]) Or(alternative *Result[V]) *Result[V]

Or returns this Result if there is no error, otherwise it returns the provided alternative Result instance.

func (*Result[V]) OrElse added in v0.24.0

func (m *Result[V]) OrElse(defaultValue V) V

OrElse returns the value if there is no error, otherwise it returns the provided default value.

func (*Result[V]) OrElseGet added in v0.24.0

func (m *Result[V]) OrElseGet(defaultValue func() V) V

OrElseGet returns the value if there is no error, otherwise it returns the result of the provided function.

func (*Result[V]) Seq added in v0.24.0

func (m *Result[V]) Seq() iter.Seq[Result[V]]

Seq returns an iter.Seq with this Result.

This is useful for reusing functions provided by package seq.

func (*Result[V]) Seq2 added in v0.24.0

func (m *Result[V]) Seq2() iter.Seq2[V, error]

Seq2 returns an iter.Seq2 with value and error.

This is useful for reusing functions provided by package seq2 or seqerr.

type Signed

type Signed interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64
}

Signed is a constraint that permits any signed integer type. If future releases of Go add new predeclared signed integer types, this constraint will be modified to include them.

type SignedNumber

type SignedNumber interface {
	Signed | Float
}

SignedNumber is a constraint that permits any signed numeric type. If future releases of Go add new predeclared signed numeric types, this constraint will be modified to include them.

type Tuple

type Tuple[A, B any] = Tuple2[A, B]

Tuple is a group of 2 elements

func NewTuple

func NewTuple[A, B any](a A, b B) Tuple[A, B]

NewTuple creates a Tuple from given values.

type Tuple2

type Tuple2[A, B any] struct {
	A A
	B B
}

Tuple2 is a group of 2 elements.

func NewTuple2

func NewTuple2[A, B any](a A, b B) Tuple2[A, B]

NewTuple2 creates a Tuple2 from given values.

func (Tuple2[A, B]) GetLeft

func (t Tuple2[A, B]) GetLeft() A

GetLeft returns the left value of the tuple.

func (Tuple2[A, B]) GetRight

func (t Tuple2[A, B]) GetRight() B

GetRight returns the right value of the tuple.

type Tuple3

type Tuple3[A, B, C any] struct {
	A A
	B B
	C C
}

Tuple3 is a group of 3 elements.

func NewTuple3

func NewTuple3[A, B, C any](a A, b B, c C) Tuple3[A, B, C]

NewTuple3 creates a Tuple3 from given values.

type Tuple4

type Tuple4[A, B, C, D any] struct {
	A A
	B B
	C C
	D D
}

Tuple4 is a group of 4 elements.

func NewTuple4

func NewTuple4[A, B, C, D any](a A, b B, c C, d D) Tuple4[A, B, C, D]

NewTuple4 creates a Tuple4 from given values.

type Tuple5

type Tuple5[A, B, C, D, E any] struct {
	A A
	B B
	C C
	D D
	E E
}

Tuple5 is a group of 5 elements.

func NewTuple5

func NewTuple5[A, B, C, D, E any](a A, b B, c C, d D, e E) Tuple5[A, B, C, D, E]

NewTuple5 creates a Tuple5 from given values.

type Tuple6

type Tuple6[A, B, C, D, E, F any] struct {
	A A
	B B
	C C
	D D
	E E
	F F
}

Tuple6 is a group of 6 elements.

func NewTuple6

func NewTuple6[A, B, C, D, E, F any](a A, b B, c C, d D, e E, f F) Tuple6[A, B, C, D, E, F]

NewTuple6 creates a Tuple6 from given values.

type Tuple7

type Tuple7[A, B, C, D, E, F, G any] struct {
	A A
	B B
	C C
	D D
	E E
	F F
	G G
}

Tuple7 is a group of 7 elements.

func NewTuple7

func NewTuple7[A, B, C, D, E, F, G any](a A, b B, c C, d D, e E, f F, g G) Tuple7[A, B, C, D, E, F, G]

NewTuple7 creates a Tuple7 from given values.

type Tuple8

type Tuple8[A, B, C, D, E, F, G, H any] struct {
	A A
	B B
	C C
	D D
	E E
	F F
	G G
	H H
}

Tuple8 is a group of 8 elements.

func NewTuple8

func NewTuple8[A, B, C, D, E, F, G, H any](a A, b B, c C, d D, e E, f F, g G, h H) Tuple8[A, B, C, D, E, F, G, H]

NewTuple8 creates a Tuple8 from given values.

type Tuple9

type Tuple9[A, B, C, D, E, F, G, H, I any] struct {
	A A
	B B
	C C
	D D
	E E
	F F
	G G
	H H
	I I
}

Tuple9 is a group of 9 elements.

func NewTuple9

func NewTuple9[A, B, C, D, E, F, G, H, I any](a A, b B, c C, d D, e E, f F, g G, h H, i I) Tuple9[A, B, C, D, E, F, G, H, I]

NewTuple9 creates a Tuple9 from given values.

type Unsigned

type Unsigned interface {
	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

Unsigned is a constraint that permits any unsigned integer type. If future releases of Go add new predeclared unsigned integer types, this constraint will be modified to include them.

Jump to

Keyboard shortcuts

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