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 ¶
- type Comparable
- type Complex
- type Float
- type Integer
- type Number
- type Ordered
- type Pair
- type PairLike
- type Result
- func (m *Result[V]) Get() (V, error)
- func (m *Result[V]) GetError() error
- func (m *Result[V]) IsError() bool
- func (m *Result[V]) IsNotError() bool
- func (m *Result[V]) MustGetError() error
- func (m *Result[V]) MustGetValue() V
- func (m *Result[V]) Or(alternative *Result[V]) *Result[V]
- func (m *Result[V]) OrElse(defaultValue V) V
- func (m *Result[V]) OrElseGet(defaultValue func() V) V
- func (m *Result[V]) Seq() iter.Seq[Result[V]]
- func (m *Result[V]) Seq2() iter.Seq2[V, error]
- type Signed
- type SignedNumber
- type Tuple
- type Tuple2
- type Tuple3
- type Tuple4
- type Tuple5
- type Tuple6
- type Tuple7
- type Tuple8
- type Tuple9
- type Unsigned
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 ¶
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 ¶
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 ¶
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 ¶
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 (*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 ¶
Seq returns an iter.Seq with this Pair.
This is useful for reusing functions provided by package seq.
func (*Pair[L, R]) Seq2 ¶
Seq2 returns an iter.Seq2 with left and right value.
This is useful for reusing functions provided by package seq2.
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
FailureResult creates a new Result instance with the provided error.
func ResultFrom ¶ added in v0.24.0
ResultFrom creates a Result instance from a function that returns a value and an error.
func ResultFromPair ¶ added in v0.24.0
ResultFromPair creates a Result instance from a PairLike argument.
func ResultOf ¶ added in v0.24.0
ResultOf creates a new Result instance with the provided value and error.
func SuccessResult ¶ added in v0.24.0
SuccessResult creates a new Result instance with the provided value.
func (*Result[V]) GetError ¶ added in v0.26.0
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
IsError checks if the Result instance contains an error.
func (*Result[V]) IsNotError ¶ added in v0.24.0
IsNotError checks if the Result instance does not contain an error.
func (*Result[V]) MustGetError ¶ added in v0.26.0
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
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.
type Signed ¶
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 ¶
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 Tuple2 ¶
type Tuple2[A, B any] struct { A A B B }
Tuple2 is a group of 2 elements.
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.
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.
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.
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.
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.