constraints

package
v0.0.0-...-89aa834 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2024 License: MIT Imports: 0 Imported by: 0

Documentation

Overview

Package constraints provides type constraints for generic programming in Go.

It defines a set of interface types that can be used as type constraints in generic functions and types. These constraints cover various numeric types and their combinations, allowing for more precise and flexible generic programming with numbers in Go.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Addable

type Addable interface {
	Number | string
}

Addable is a constraint that permits any number or string type.

Example
package main

import (
	"fmt"

	"github.com/gopherd/core/constraints"
)

func sum[T constraints.Addable](values ...T) T {
	var result T
	for _, v := range values {
		result += v
	}
	return result
}

func main() {
	fmt.Println(sum(1, 2))
	fmt.Println(sum("hello", " ", "world"))
}
Output:

3
hello world

type Complex

type Complex interface {
	~complex64 | ~complex128
}

Complex is a constraint that permits any complex numeric type.

type Field

type Field interface {
	Float | Complex
}

Field is a constraint that permits any number field type.

type Float

type Float interface {
	~float32 | ~float64
}

Float is a constraint that permits any floating-point type.

type Integer

type Integer interface {
	Signed | Unsigned
}

Integer is a constraint that permits any integer type.

type Number

type Number interface {
	Real | Complex
}

Number is a constraint that permits any numeric type.

type Real

type Real interface {
	Integer | Float
}

Real is a constraint that permits any real number type.

type Signed

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

Signed is a constraint that permits any signed integer type.

type SignedNumber

type SignedNumber interface {
	SignedReal | Complex
}

SignedNumber is a constraint that permits any signed numeric type.

type SignedReal

type SignedReal interface {
	Signed | Float
}

SignedReal is a constraint that permits any signed real number type.

type Unsigned

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

Unsigned is a constraint that permits any unsigned integer type.

Jump to

Keyboard shortcuts

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