pseudo

package
v1.13.14-0.1.0-rc.1 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2024 License: GPL-3.0 Imports: 5 Imported by: 0

Documentation

Overview

Package pseudo provides a bridge between generic and non-generic code via pseudo-types and pseudo-values. With careful usage, there is minimal reduction in type safety.

Adding generic type parameters to anything (e.g. struct, function, etc) "pollutes" all code that uses the generic type. Refactoring all uses isn't always feasible, and a Type acts as an intermediate fix. Although their constructors are generic, they are not, and they are instead coupled with a generic Value that SHOULD be used for access.

Packages typically SHOULD NOT expose a Type and SHOULD instead provide users with a type-safe Value.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Constructor

type Constructor interface {
	Zero() *Type
	NewPointer() *Type
	NilPointer() *Type
}

A Constructor returns newly constructed Type instances for a pre-registered concrete type.

func NewConstructor

func NewConstructor[T any]() Constructor

NewConstructor returns a Constructor that builds `T` Type instances.

type EqualityChecker

type EqualityChecker[T any] interface {
	Equal(T) bool
}

An EqualityChecker reports if it is equal to another value of the same type.

type Pseudo

type Pseudo[T any] struct {
	Type  *Type
	Value *Value[T]
}

A Pseudo type couples a Type and a Value. If returned by a constructor from this package, both wrap the same payload.

func From

func From[T any](v T) *Pseudo[T]

From returns a Pseudo[T] constructed from `v`.

func MustPointerTo

func MustPointerTo[T any](t *Type) *Pseudo[*T]

MustPointerTo is equivalent to PointerTo except that it panics instead of returning an error.

func PointerTo

func PointerTo[T any](t *Type) (*Pseudo[*T], error)

PointerTo is equivalent to From called with a pointer to the payload carried by `t`. It first confirms that the payload is of type `T`.

func Zero

func Zero[T any]() *Pseudo[T]

Zero is equivalent to From called with the zero value of type `T`. Note that pointers, slices, maps, etc. will therefore be nil.

func (*Pseudo[T]) TypeAndValue

func (p *Pseudo[T]) TypeAndValue() (*Type, *Value[T])

TypeAndValue is a convenience function for splitting the contents of `p`, typically at construction.

Example
typ, val := From("hello").TypeAndValue()

// But, if only one is needed:
typ = From("world").Type
val = From("this isn't coupled to the Type").Value

_ = typ
_ = val
Output:

type Type

type Type struct {
	// contains filtered or unexported fields
}

A Type wraps a strongly-typed value without exposing information about its type. It can be used in lieu of a generic field / parameter.

func (*Type) DecodeRLP

func (t *Type) DecodeRLP(s *rlp.Stream) error

DecodeRLP implements the rlp.Decoder interface.

func (*Type) EncodeRLP

func (t *Type) EncodeRLP(w io.Writer) error

EncodeRLP implements the rlp.Encoder interface.

func (*Type) Equal

func (t *Type) Equal(u *Type) bool

Equal reports whether t carries a value equal to that carried by u. If t and u carry different types then Equal returns false. If t and u carry the same type and said type implements EqualityChecker then Equal propagates the value returned by the checker. In all other cases, Equal returns reflect.DeepEqual performed on the payloads carried by t and u.

func (*Type) Format

func (t *Type) Format(s fmt.State, verb rune)

Format implements the fmt.Formatter interface.

func (*Type) Interface

func (t *Type) Interface() any

Interface returns the wrapped value as an `any`, equivalent to reflect.Value.Interface. Prefer Value.Get.

func (*Type) IsZero

func (t *Type) IsZero() bool

IsZero reports whether t carries the the zero value for its type.

func (*Type) MarshalJSON

func (t *Type) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (*Type) UnmarshalJSON

func (t *Type) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

type Value

type Value[T any] struct {
	// contains filtered or unexported fields
}

A Value provides strongly-typed access to the payload carried by a Type.

func MustNewValue

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

MustNewValue is equivalent to NewValue except that it panics instead of returning an error.

func NewValue

func NewValue[T any](t *Type) (*Value[T], error)

NewValue constructs a Value from a Type, first confirming that `t` wraps a payload of type `T`.

func (*Value[T]) Format

func (v *Value[T]) Format(s fmt.State, verb rune)

Format implements the fmt.Formatter interface.

func (*Value[T]) Get

func (v *Value[T]) Get() T

Get returns the value.

func (*Value[T]) MarshalJSON

func (v *Value[T]) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (*Value[T]) Set

func (v *Value[T]) Set(val T)

Set sets the value.

func (*Value[T]) UnmarshalJSON

func (v *Value[T]) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

Jump to

Keyboard shortcuts

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