types
import "github.com/go-softwarelab/common/pkg/types"
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.
type 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 Comparable = comparable
type Complex
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 Complex interface {
// contains filtered or unexported methods
}
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 Float interface {
// contains filtered or unexported methods
}
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 Integer interface {
// contains filtered or unexported methods
}
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 Number interface {
// contains filtered or unexported methods
}
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 Ordered = cmp.Ordered
type Pair
Pair is a generic type that represents a pair of values.
type Pair[L, R any] struct {
Left L
Right R
}
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.
*Pair[L, R].GetLeft
func (p *Pair[L, R]) GetLeft() L
GetLeft returns the left value of the pair.
*Pair[L, R].GetRight
func (p *Pair[L, R]) GetRight() R
GetRight returns the right value of the pair.
*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.
*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.
*Pair[L, R].ToTuple
func (p *Pair[L, R]) ToTuple() Tuple2[L, R]
ToTuple converts the Pair to a Tuple.
*Pair[L, R].Unpack
func (p *Pair[L, R]) Unpack() (L, R)
Unpack returns the left and right values of the pair.
type PairLike
PairLike represents any type that is pair-like, it is used for creating a Result instance.
type PairLike[L, R any] interface {
GetLeft() L
GetRight() R
}
type Result
Result is a type representing a result that could be either a value or an error.
type Result[V any] struct {
// contains filtered or unexported fields
}
FailureResult
func FailureResult[V any](err error) *Result[V]
FailureResult creates a new Result instance with the provided error.
ResultFrom
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.
ResultFromPair
func ResultFromPair[V any](pair PairLike[V, error]) *Result[V]
ResultFromPair creates a Result instance from a PairLike argument.
ResultOf
func ResultOf[V any](value V, err error) *Result[V]
ResultOf creates a new Result instance with the provided value and error.
SuccessResult
func SuccessResult[V any](value V) *Result[V]
SuccessResult creates a new Result instance with the provided value.
*Result[V].Get
func (m *Result[V]) Get() (V, error)
Get returns the value and error from the Result instance.
*Result[V].GetError
func (m *Result[V]) GetError() error
GetError returns the error from the Result instance. If there is no error, it returns nil.
*Result[V].IsError
func (m *Result[V]) IsError() bool
IsError checks if the Result instance contains an error.
*Result[V].IsNotError
func (m *Result[V]) IsNotError() bool
IsNotError checks if the Result instance does not contain an error.
*Result[V].MustGetError
func (m *Result[V]) MustGetError() error
MustGetError returns the error from the Result instance, panicking if there is no error.
*Result[V].MustGetValue
func (m *Result[V]) MustGetValue() V
MustGetValue returns the value from the Result instance, panicking if there is an error.
*Result[V].Or
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.
*Result[V].OrElse
func (m *Result[V]) OrElse(defaultValue V) V
OrElse returns the value if there is no error, otherwise it returns the provided default value.
*Result[V].OrElseGet
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.
*Result[V].Seq
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.
*Result[V].Seq2
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
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 Signed interface {
// contains filtered or unexported methods
}
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 SignedNumber interface {
// contains filtered or unexported methods
}
type Tuple
Tuple is a group of 2 elements
type Tuple[A, B any] = Tuple2[A, B]
NewTuple
func NewTuple[A, B any](a A, b B) Tuple[A, B]
NewTuple creates a Tuple from given values.
type Tuple2
Tuple2 is a group of 2 elements.
type Tuple2[A, B any] struct {
A A
B B
}
NewTuple2
func NewTuple2[A, B any](a A, b B) Tuple2[A, B]
NewTuple2 creates a Tuple2 from given values.
Tuple2[A, B].GetLeft
func (t Tuple2[A, B]) GetLeft() A
GetLeft returns the left value of the tuple.
Tuple2[A, B].GetRight
func (t Tuple2[A, B]) GetRight() B
GetRight returns the right value of the tuple.
type Tuple3
Tuple3 is a group of 3 elements.
type Tuple3[A, B, C any] struct {
A A
B B
C C
}
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
Tuple4 is a group of 4 elements.
type Tuple4[A, B, C, D any] struct {
A A
B B
C C
D D
}
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
Tuple5 is a group of 5 elements.
type Tuple5[A, B, C, D, E any] struct {
A A
B B
C C
D D
E E
}
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
Tuple6 is a group of 6 elements.
type Tuple6[A, B, C, D, E, F any] struct {
A A
B B
C C
D D
E E
F F
}
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
Tuple7 is a group of 7 elements.
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
}
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
Tuple8 is a group of 8 elements.
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
}
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
Tuple9 is a group of 9 elements.
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
}
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
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.
type Unsigned interface {
// contains filtered or unexported methods
}