---
title: Std.Dynamic
---

Type-safe decoding of dynamically typed BEAM values.

## Types

### Dynamic

```saga
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

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

An error produced when decoding a Dynamic value fails.

### Decoder

```saga
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

```saga
fun string : Decoder String
```

Decoder for String values.

### int

```saga
fun int : Decoder Int
```

Decoder for Int values.

### float

```saga
fun float : Decoder Float
```

Decoder for Float values.

### bool

```saga
fun bool : Decoder Bool
```

Decoder for Bool values.

### classify

```saga
fun classify : Dynamic -> String
```

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

### from_erlang

```saga
fun from_erlang : a -> Dynamic
```

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

### decode

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

Apply a decoder to a Dynamic value.

### decode_field

```saga
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

```saga
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

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

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

### optional

```saga
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.

