generichelper

package module
v0.18.0 Latest Latest
Warning

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

Go to latest
Published: May 5, 2024 License: MIT Imports: 1 Imported by: 14

README

generichelper

Go Reference

Helpers for Go's generics.

Documentation

Overview

Package generichelper contains generics-related helpers.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeepEqual

func DeepEqual[T any](x, y T) bool

DeepEqual is a generic wrapper around reflect.DeepEqual.

func Is added in v0.10.0

func Is[T any](x any) bool

Is determines whether 'x' is of type T.

func IsNoType added in v0.14.0

func IsNoType[T any]() bool

IsNoType determines whether T's type is NoType.

func IsZeroValue added in v0.18.0

func IsZeroValue[T any](t T) bool

IsZeroValue determines whether 't' is of zero value.

func SameType added in v0.16.0

func SameType[T, O any]() bool

SameType determines whether T's and O's types are the same.

Example (String)
fmt.Println(SameType[string, NoType]())
fmt.Println(SameType[string, string]())
fmt.Println(SameType[int, string]())
type s2 = string
fmt.Println(SameType[string, s2]())
type s3 string
fmt.Println(SameType[s3, string]())
Output:

false
true
false
true
false

func Ternary added in v0.5.0

func Ternary[T any](condition bool, trueT, falseT T) T

Ternary mimics ternary conditional operation.

func TypeHoldsType added in v0.3.0

func TypeHoldsType[T, O any]() bool

TypeHoldsType reports whether type T holds other type O.

TypeHoldsType returns 'true' if:

Otherwise, TypeHoldsType returns 'false'.

func TypeMeetsType added in v0.16.0

func TypeMeetsType[T, O any]() bool

TypeMeetsType returns 'true' if:

Otherwise, TypeMeetsType returns 'false'.

func TypeOf deprecated added in v0.12.0

func TypeOf[T any]() reflect.Type

TypeOf returns T's reflect.Type.

Deprecated: Use reflect.TypeFor instead.

func ZeroValue

func ZeroValue[T any]() T

ZeroValue returns T's zero value.

Types

type NoType added in v0.13.0

type NoType struct{}

NoType is a sentinel type to denote that this type parameter needs not to be processed (see ExampleNoType). NoType may be used to instantiate type parameter with 'any' or 'comparable' type constraint only.

Example
fmt.Println(testNoType_any[NoType]())
fmt.Println(testNoType_any[struct{}]())
fmt.Println(testNoType_comparable[NoType]())
fmt.Println(testNoType_comparable[int]())
Output:

NoType
struct
NoType
int

type Tuple2 added in v0.6.0

type Tuple2[T1, T2 any] struct {
	Item1 T1
	Item2 T2
}

Tuple2 represents a 2-tuple, or pair.

func NewTuple2 added in v0.17.0

func NewTuple2[T1, T2 any](item1 T1, item2 T2) Tuple2[T1, T2]

NewTuple2 creates Tuple2.

Jump to

Keyboard shortcuts

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