constraints

package
v1.20.4 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2025 License: MIT Imports: 0 Imported by: 0

Documentation

Overview

Package constraints provides type constraints for generic programming.

This package defines numeric type constraints used throughout the gust library for generic type parameters, including PureInteger, Integer, and Digit constraints.

Package constraints provides type constraints for generic programming.

This package defines type constraints used throughout the gust library for generic type parameters, including ordering, numeric, and comparison constraints.

Examples

// Use Ordering for comparisons
ord := constraints.Compare(1, 2)
if ord.IsLess() {
	fmt.Println("1 is less than 2")
}

// Use constraints in generic functions
func max[T constraints.Ord](a, b T) T {
	if constraints.Compare(a, b).IsGreater() {
		return a
	}
	return b
}

// Use numeric constraints
func sum[T constraints.Digit](values []T) T {
	var total T
	for _, v := range values {
		total += v
	}
	return total
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Digit

type Digit interface {
	Integer | ~float32 | ~float64
}

Digit represents numeric types including integers and floating-point numbers.

type Integer

type Integer interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64
}

Integer represents integer types including type aliases.

type Ord

type Ord interface {
	Digit | ~string | ~uintptr
}

Ord represents types that can be ordered (compared).

type Ordering

type Ordering struct {
	// contains filtered or unexported fields
}

Ordering represents the result of a comparison between two values.

func Compare

func Compare[T Ord](a, b T) Ordering

Compare compares two values and returns their Ordering.

func Equal

func Equal() Ordering

Equal returns an Ordering representing "equal".

func Greater

func Greater() Ordering

Greater returns an Ordering representing "greater than".

func Less

func Less() Ordering

Less returns an Ordering representing "less than".

func (Ordering) Is

func (o Ordering) Is(ord Ordering) bool

Is checks if this Ordering matches the given Ordering.

func (Ordering) IsEqual

func (o Ordering) IsEqual() bool

IsEqual returns true if this Ordering represents "equal".

func (Ordering) IsGreater

func (o Ordering) IsGreater() bool

IsGreater returns true if this Ordering represents "greater than".

func (Ordering) IsLess

func (o Ordering) IsLess() bool

IsLess returns true if this Ordering represents "less than".

type PureInteger

type PureInteger interface {
	int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64
}

PureInteger represents pure integer types without type aliases.

Jump to

Keyboard shortcuts

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