SagaSaga
Standard Library

Std.Dynamic

Type-safe decoding of dynamically typed BEAM values.

Types

Dynamic

opaque type Dynamic

An opaque type wrapping a raw Erlang/BEAM term. Used at FFI boundaries where the type of a value is unknown at compile time.

DecodeError

type DecodeError =
  | DecodeError (expected: String) (found: String) (List String)
  deriving (Eq)

An error produced when decoding a Dynamic value fails.

Decoder

type Decoder a =
  | Decoder (Dynamic -> Result a DecodeError)

A decoder is a function from a Dynamic to a typed value or a DecodeError. Decoders are pure values — composable, introspection-free, no placeholders.

Functions

string

fun string : Decoder String

Decoder for String values.

int

fun int : Decoder Int

Decoder for Int values.

float

fun float : Decoder Float

Decoder for Float values.

bool

fun bool : Decoder Bool

Decoder for Bool values.

classify

fun classify : Dynamic -> String

Classify a Dynamic value, returning a human-readable type name. Useful for error messages.

from_erlang

fun from_erlang : a -> Dynamic

Wrap any value as Dynamic. This is an identity function at runtime — it just erases the type information.

decode

fun decode : Decoder a -> Dynamic -> Result a DecodeError

Apply a decoder to a Dynamic value.

decode_field

fun decode_field : String -> Decoder a -> Dynamic -> Result a DecodeError

Look up a field by name in a map-like Dynamic and decode it. Returns Err if the field is missing or its value fails to decode.

decode_element

fun decode_element : Int -> Decoder a -> Dynamic -> Result a DecodeError

Look up an element by index in a tuple/list-like Dynamic and decode it. Returns Err if the index is out of bounds or its value fails to decode.

list_of

fun list_of : Decoder a -> Decoder (List a)

Decoder for a list where every element is decoded with the given decoder.

optional

fun optional : Decoder a -> Decoder (Maybe a)

Decoder for optional/nullable values. Decodes nil/undefined/null as Nothing, otherwise tries the inner decoder and wraps in Just.