genericsutil

package
v0.83.0 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2026 License: BSD-3-Clause Imports: 1 Imported by: 0

Documentation

Overview

Package genericsutil provides helpers for implementing type erasure patterns

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertOrZero

func AssertOrZero[T any](v any) T

Returns v cast as type T if possible, otherwise returns the zero value of T

func IsNone

func IsNone(v any) bool

Returns true if v is either an empty struct or a pointer to an empty struct

func Zero

func Zero[T any]() T

Returns the zero value of type T

Types

type AnyIOFunc

type AnyIOFunc interface {
	AnyZeroHelper

	// If type assertion fails, implementation should still call the
	// underlying function with the zero value of the input type.
	ExecuteLoose(any) (any, error)

	// If type assertion fails, implementation should return an error
	// and the zero value of the input type, without calling the
	// underlying function.
	ExecuteStrict(any) (any, error)
}

type AnyZeroHelper

type AnyZeroHelper interface {
	I() any    // returns direct I zero val
	O() any    // returns direct O zero val
	IPtr() any // returns `new(I)` (pointer to I)
	OPtr() any // returns `new(O)` (pointer to O)
}

type IOFunc

type IOFunc[I any, O any] func(I) (O, error)

func (IOFunc[I, O]) ExecuteLoose

func (ioFunc IOFunc[I, O]) ExecuteLoose(input any) (any, error)

If type assertion fails, ExecuteLoose will still call the underlying function with the zero value of the input type.

func (IOFunc[I, O]) ExecuteStrict

func (ioFunc IOFunc[I, O]) ExecuteStrict(input any) (any, error)

If type assertion fails, ExecuteStrict will return an error and the zero value of the input type, without calling the underlying function.

func (IOFunc[I, O]) I

func (IOFunc[I, O]) I() any

func (IOFunc[I, O]) IPtr

func (IOFunc[I, O]) IPtr() any

func (IOFunc[I, O]) O

func (IOFunc[I, O]) O() any

func (IOFunc[I, O]) OPtr

func (IOFunc[I, O]) OPtr() any

type None

type None = struct{}

Simple alias for an empty struct

type ZeroHelper

type ZeroHelper[I any, O any] struct{}

func (ZeroHelper[I, O]) I

func (ZeroHelper[I, O]) I() any

func (ZeroHelper[I, O]) IPtr

func (ZeroHelper[I, O]) IPtr() any

func (ZeroHelper[I, O]) O

func (ZeroHelper[I, O]) O() any

func (ZeroHelper[I, O]) OPtr

func (ZeroHelper[I, O]) OPtr() any

Jump to

Keyboard shortcuts

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