generics

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Deprecated: use appropriate sub packages in github.com/mandelsoft/goutils

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendedSlice added in v0.4.0

func AppendedSlice[E any](slice []E, elems ...E) []E

func As added in v0.3.0

func As[T any](o interface{}) T

As casts an element typed by a type parameter to a subtype, given by the type parameter T.

func AsE added in v0.3.0

func AsE[T any](o interface{}, err error) (T, error)

AsE casts a pointer/error result to an interface/error result. In Go this cannot be done directly, because returning a nil pinter for an interface return type, would result is a typed nil value for the interface, and not nil, if the pointer is nil. Unfortunately, the relation of the pointer (even the fact, that a pointer is expected)to the interface (even the fact, that an interface is expected) cannot be expressed with Go generics.

func AsI added in v0.4.1

func AsI[T any](p any) T

AsI converts a struct pointer to an appropriate interface type given as type parameter. In Go this cannot be done directly, because returning a nil pinter for an interface return type, would result is a typed nil value for the interface, and not nil, if the pointer is nil. Unfortunately, the relation of the pointer (even the fact, that a pointer is expected)to the interface (even the fact, that an interface is expected) cannot be expressed with Go generics.

func Cast added in v0.3.0

func Cast[S any](o any) (S, error)

Cast casts one type parameter to another type parameter, which have a sub type relation. This cannot be described by type parameter constraints in Go, because constraints may not be type parameters again.

func Conditional

func Conditional[T any](cond bool, a, b T) T

func ConvertSlice added in v0.3.0

func ConvertSlice[S, T any](in ...S) []T

ConvertSlice Converts the element typ of a slice. hereby, Type S must be a subtype of type T.

func ConvertSliceTo added in v0.4.1

func ConvertSliceTo[T, S any](in []S) []T

ConvertSliceTo Converts the element typ of a slice. hereby, Type S must be a subtype of type T. typically, S can be omitted when used, because it can be derived from the argument.

func ConvertSliceWith added in v0.4.1

func ConvertSliceWith[S, T, I any](c func(I) T, in []S) []T

ConvertSliceWith converts the element type of a slice using a converter function. Unfortunately this cannot be expressed in a type-safe way in Go. I MUST follow the type constraint I super S, which cannot be expressed in Go.

func KeyList added in v0.4.1

func KeyList[K Comparable[K], V any](m map[K]V) []K

func MapKeyArray added in v0.3.0

func MapKeyArray[K comparable, V any](m map[K]V) []K

func MapValues added in v0.3.0

func MapValues[K comparable, V any](m map[K]V) []V

func Pointer added in v0.3.0

func Pointer[T any](t T) *T

func TryCast added in v0.3.0

func TryCast[T any](p any) (T, bool)

TryCast tries a type cast usable for variable with a parametric type.

func TypeOf added in v0.4.1

func TypeOf[T any]() reflect.Type

TypeOf returns the reflect.Type object for a formal Go type given by type parameter.

func Value added in v0.3.0

func Value[T any](t *T) T

Types

type Comparable added in v0.4.1

type Comparable[K any] interface {
	comparable
	Compare(o K) int
}

type Set

type Set[K comparable] map[K]struct{}

func KeySet added in v0.2.0

func KeySet[K comparable, V any](m map[K]V) Set[K]

func MapKeys added in v0.3.0

func MapKeys[K comparable, V any](m map[K]V) Set[K]

func NewSet

func NewSet[K comparable](keys ...K) Set[K]

func (Set[K]) Add

func (s Set[K]) Add(keys ...K) Set[K]

func (Set[K]) AsArray added in v0.3.0

func (s Set[K]) AsArray() []K

func (Set[K]) Contains

func (s Set[K]) Contains(keys ...K) bool

func (Set[K]) Delete

func (s Set[K]) Delete(keys ...K) Set[K]

Jump to

Keyboard shortcuts

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