constraints

package
v0.12.2 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2024 License: AGPL-3.0 Imports: 0 Imported by: 0

Documentation

Overview

Package constraints provides some basic constraints that can be used by other types and functions and embedded in other constraints.

Currently, this package provides constraints for numeric types (including signed and unsigned integers, floating-point numbers, and complex numbers), byte sequence types (including byte slices and strings), slices, and maps.

These constraints are helpful to apply arithmetic operators and comparison operators in generic code.

The arithmetic operators include sum (+), difference (-), product (*), quotient (/), remainder (%), bitwise AND (&), bitwise OR (|), bitwise XOR (^), bit clear (AND NOT, &^), left shift (<<), and right shift (>>). See <https://go.dev/ref/spec#Arithmetic_operators> for details.

The comparison operators include equal (==), not equal (!=), less (<), less or equal (<=), greater (>), greater or equal (>=). See <https://go.dev/ref/spec#Comparison_operators> for details.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Addable

type Addable interface {
	Numeric | ~string
}

Addable is a constraint that matches any addable type.

An addable type is one that supports the + operator.

type ByteString added in v0.6.0

type ByteString interface {
	~[]byte | ~string
}

ByteString is a constraint for byte strings. It matches any type whose underlying type is []byte or string.

type Complex

type Complex interface {
	~complex64 | ~complex128
}

Complex is a constraint for complex numbers. It matches any type whose underlying type is complex64 or complex128.

type Float

type Float interface {
	~float32 | ~float64
}

Float is a constraint for floating-point numbers. It matches any type whose underlying type is float32 or float64.

type Integer

type Integer interface {
	SignedInteger | UnsignedInteger
}

Integer is a constraint for integers. It matches any type whose underlying type is one of int, int8, int16, int32 (rune), int64, uint, uint8 (byte), uint16, uint32, uint64, and uintptr.

type Map added in v0.11.0

type Map[Key comparable, Value any] interface {
	~map[Key]Value
}

Map is a constraint for maps. It matches any type whose underlying type is map[Key]Value, where Key is a comparable type and Value can be any type.

type Numeric

type Numeric interface {
	Real | Complex
}

Numeric is a constraint for numerics. It matches any type whose underlying type is one of int, int8, int16, int32 (rune), int64, uint, uint8 (byte), uint16, uint32, uint64, uintptr, float32, float64, complex64, and complex128.

type Ordered

type Ordered interface {
	Real | ~string
}

Ordered is a constraint that matches any ordered type.

An ordered type is one that supports the <, <=, >, and >= operators.

type PredeclaredAddable

type PredeclaredAddable interface {
	PredeclaredNumeric | string
}

PredeclaredAddable is a constraint that matches the predeclared addable types: int, int8, int16, int32 (rune), int64, uint, uint8 (byte), uint16, uint32, uint64, uintptr, float32, float64, complex64, complex128, and string.

An addable type is one that supports the + operator.

type PredeclaredByteString added in v0.6.0

type PredeclaredByteString interface {
	[]byte | string
}

PredeclaredByteString is a constraint that matches the predeclared byte string types: []byte and string.

type PredeclaredComplex

type PredeclaredComplex interface {
	complex64 | complex128
}

PredeclaredComplex is a constraint that matches the two predeclared complex number types: complex64 and complex128.

type PredeclaredFloat

type PredeclaredFloat interface {
	float32 | float64
}

PredeclaredFloat is a constraint that matches the two predeclared floating-point number types: float32 and float64.

type PredeclaredInteger

type PredeclaredInteger interface {
	PredeclaredSignedInteger | PredeclaredUnsignedInteger
}

PredeclaredInteger is a constraint that matches the eleven predeclared integer types: int, int8, int16, int32 (rune), int64, uint, uint8 (byte), uint16, uint32, uint64, and uintptr.

type PredeclaredNumeric

type PredeclaredNumeric interface {
	PredeclaredReal | PredeclaredComplex
}

PredeclaredNumeric is a constraint that matches the predeclared numeric types: int, int8, int16, int32 (rune), int64, uint, uint8 (byte), uint16, uint32, uint64, uintptr, float32, float64, complex64, and complex128.

type PredeclaredOrdered

type PredeclaredOrdered interface {
	PredeclaredReal | string
}

PredeclaredOrdered is a constraint that matches the predeclared ordered types: int, int8, int16, int32 (rune), int64, uint, uint8 (byte), uint16, uint32, uint64, uintptr, float32, float64, and string.

An ordered type is one that supports the <, <=, >, and >= operators.

type PredeclaredReal

type PredeclaredReal interface {
	PredeclaredInteger | PredeclaredFloat
}

PredeclaredReal is a constraint that matches the predeclared real number types: int, int8, int16, int32 (rune), int64, uint, uint8 (byte), uint16, uint32, uint64, uintptr, float32 and float64.

type PredeclaredSignedInteger

type PredeclaredSignedInteger interface {
	int | int8 | int16 | int32 | int64
}

PredeclaredSignedInteger is a constraint that matches the five predeclared signed integer types: int, int8, int16, int32 (rune), and int64.

type PredeclaredTransitiveOrdered

type PredeclaredTransitiveOrdered interface {
	PredeclaredInteger | string
}

PredeclaredTransitiveOrdered is a constraint that matches the predeclared ordered types that implement a transitive ordering, including int, int8, int16, int32 (rune), int64, uint, uint8 (byte), uint16, uint32, uint64, uintptr, and string.

An ordered type is one that supports the <, <=, >, and >= operators.

A transitive ordering satisfies:

  • if both a < b and b < c are true, then a < c must be true as well.
  • if both a < b and b < c are false, then a < c must be false as well.

type PredeclaredUnsignedInteger

type PredeclaredUnsignedInteger interface {
	uint | uint8 | uint16 | uint32 | uint64 | uintptr
}

PredeclaredUnsignedInteger is a constraint that matches the six predeclared unsigned integer types: uint, uint8 (byte), uint16, uint32, uint64, and uintptr.

type Real

type Real interface {
	Integer | Float
}

Real is a constraint for real numbers. It matches any type whose underlying type is one of int, int8, int16, int32 (rune), int64, uint, uint8 (byte), uint16, uint32, uint64, uintptr, float32, and float64.

type SignedInteger

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

SignedInteger is a constraint for signed integers. It matches any type whose underlying type is one of int, int8, int16, int32 (rune), and int64.

type Slice added in v0.11.0

type Slice[Elem any] interface {
	~[]Elem
}

Slice is a constraint for slices. It matches any type whose underlying type is []Elem, where Elem can be any type.

type TransitiveOrdered

type TransitiveOrdered interface {
	Integer | ~string
}

TransitiveOrdered is a constraint that matches any ordered type that implements a transitive ordering.

An ordered type is one that supports the <, <=, >, and >= operators.

A transitive ordering satisfies:

  • if both a < b and b < c are true, then a < c must be true as well.
  • if both a < b and b < c are false, then a < c must be false as well.

type UnsignedInteger

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

UnsignedInteger is a constraint for unsigned integers. It matches any type whose underlying type is one of uint, uint8 (byte), uint16, uint32, uint64, and uintptr.

Jump to

Keyboard shortcuts

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