Plushie.Type.Composite behaviour (Plushie v0.7.0)

Copy Markdown View Source

Behaviour for composite type modules.

Composite types are parameterized by a spec that describes their inner structure (e.g., the element type for lists, the field specs for maps). Each composite module implements this behaviour to handle casting, typespec generation, guards, and display for its specific parameterization.

Callbacks that recurse into inner types receive a resolver function from the caller, keeping composite modules decoupled from the type resolution system.

Implementations

Summary

Callbacks

Casts a user-facing value against the composite spec.

Returns quoted typespec AST for accepted input forms. Defaults to typespec/2.

Decodes a wire-format value against the composite spec.

Returns a human-readable type string for documentation.

Returns a quoted guard expression, or nil if no guard applies.

Returns quoted typespec AST for the canonical form. The resolver maps inner type refs to their AST.

Returns true if the spec is structurally valid.

Types

spec()

@type spec() :: term()

Callbacks

cast(spec, term)

@callback cast(spec(), term()) :: {:ok, term()} | :error

Casts a user-facing value against the composite spec.

castable(spec, type_resolver)

(optional)
@callback castable(spec(), type_resolver :: (term() -> Macro.t())) :: Macro.t()

Returns quoted typespec AST for accepted input forms. Defaults to typespec/2.

decode(spec, term)

(optional)
@callback decode(spec(), term()) :: {:ok, term()} | :error

Decodes a wire-format value against the composite spec.

display_string(spec, string_resolver)

@callback display_string(spec(), string_resolver :: (term() -> String.t())) :: String.t()

Returns a human-readable type string for documentation.

guard(spec, var)

@callback guard(spec(), var :: Macro.t()) :: Macro.t() | nil

Returns a quoted guard expression, or nil if no guard applies.

typespec(spec, type_resolver)

@callback typespec(spec(), type_resolver :: (term() -> Macro.t())) :: Macro.t()

Returns quoted typespec AST for the canonical form. The resolver maps inner type refs to their AST.

valid_spec?(spec, checker)

@callback valid_spec?(spec(), checker :: (term() -> boolean())) :: boolean()

Returns true if the spec is structurally valid.