casters

package
v0.0.2-alpha.3 Latest Latest
Warning

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

Go to latest
Published: Jan 27, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertedTypes

func AssertedTypes[G any](values ...any) []G

AssertedTypes casts the provided values to the type G and returns a slice of type G. Panics if any value is not of type G.

func Bytes

func Bytes(value any) ([]byte, error)

Bytes casts the provided value to a byte slice and returns it. It returns an error if the value is not of a type that can be cast to a byte slice.

func NaturalComparable

func NaturalComparable[C comparable](value any) (cmp C, ok bool)

NaturalComparable casts the provided values to the type T as value.

Natural comparable types are the types that are comparable by default, such as primitives, strings, booleans, and complex numbers. See the documentation for the comparable type for more information. https://go.dev/blog/comparable

func Numeric

func Numeric[T nums.Any](value any) (T, error)

Numeric casts the provided value to the type T and returns it. T must be a numeric type. If the value is not of type T, an error is returned.

func Primitive

func Primitive[T prim.Any](value any) (T, error)

Primitive casts the provided value to the type T and returns it. T must be a primitive type. If the value is not of type G, an error is returned.

func PrimitiveComparable

func PrimitiveComparable[T prim.Comparable](value any) (T, error)

PrimitiveComparable casts the provided value to the type T and returns it. T must be a primitive comparable type. If the value is not of type G, an error is returned.

func PrimitiveOrdered

func PrimitiveOrdered[T prim.Ordered](value any) (T, error)

PrimitiveOrdered casts the provided value to the type T and returns it. T must be a primitive ordered type. If the value is not of type G, an error is returned.

func Type

func Type[T any](value any) (cast T, ok bool)

Type casts the provided value to the type T and returns it. It just feels stupid to have a casters package and do not have its most basic use cases.

func Types

func Types[G any](values ...any) (result []G, allMatches bool)

Types casts the provided values to the type G and returns a slice of type G. The second return value allMatches is true if all values were successfully cast to type G, and false otherwise.

Types

This section is empty.

Jump to

Keyboard shortcuts

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