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 ¶
- type Constructor
- type EqualityChecker
- type Pseudo
- type Type
- func (t *Type) DecodeRLP(s *rlp.Stream) error
- func (t *Type) EncodeRLP(w io.Writer) error
- func (t *Type) Equal(u *Type) bool
- func (t *Type) Format(s fmt.State, verb rune)
- func (t *Type) Interface() any
- func (t *Type) IsZero() bool
- func (t *Type) MarshalJSON() ([]byte, error)
- func (t *Type) UnmarshalJSON(b []byte) error
- type Value
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Constructor ¶
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 ¶
An EqualityChecker reports if it is equal to another value of the same type.
type Pseudo ¶
A Pseudo type couples a Type and a Value. If returned by a constructor from this package, both wrap the same payload.
func MustPointerTo ¶
MustPointerTo is equivalent to PointerTo except that it panics instead of returning an error.
func PointerTo ¶
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 ¶
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 ¶
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 ¶
DecodeRLP implements the rlp.Decoder interface.
func (*Type) EncodeRLP ¶
EncodeRLP implements the rlp.Encoder interface.
func (*Type) Equal ¶
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 ¶
Format implements the fmt.Formatter interface.
func (*Type) Interface ¶
Interface returns the wrapped value as an `any`, equivalent to reflect.Value.Interface. Prefer Value.Get.
func (*Type) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface.
func (*Type) UnmarshalJSON ¶
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 ¶
MustNewValue is equivalent to NewValue except that it panics instead of returning an error.
func NewValue ¶
NewValue constructs a Value from a Type, first confirming that `t` wraps a payload of type `T`.
func (*Value[T]) Format ¶
Format implements the fmt.Formatter interface.
func (*Value[T]) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface.
func (*Value[T]) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface.