# ADR 0024: Errors as values **Status:** Accepted by the project owner — 2026-10-11 (issue #87, PR #90); still needs engineering review (#44). It completes the error-handling part of [ADR 0001](0001-language-surface.md), which says only that panics are not tracked in effect rows. ## Context Issue #87 asks for errors as values: a result type you `match` on, `Ok` or `Error`. The prelude already had `Result { Ok(a) Error(e) }`, and `std/result` had `map`, `try` (for `use`) and `unwrap_or`. What was missing: - **a convention.** The standard library and the packages each signalled failure in their own way. `string.to_int` returned `Option`, `argon2.verify` returned `Bool` (a malformed hash looked like a wrong password), the demo's `add_user` returned `False` for a taken name, and `qsort` panicked when its C library ran out of memory. - **a small library.** Without `map_error`, `all` or a conversion from `Option`, code fell back to nested `match`es, so the convention was not cheap to follow. - **prelude parity with `Option`:** `Result` had `Eq`, `Show` and `Debug`, but not `Ord` or `Hash`, so it could not be a dictionary key or be sorted. ## Decision ### The convention | A function... | returns | Example | |---|---|---| | can fail, and the caller should handle why | `Result` | `string.to_int`, `net.connect`, `argon2.verify` | | may find nothing, and nothing is not a failure | `Option` | `dict.get`, `http.header`, `os.env` | | answers a yes-or-no question | `Bool` | `string.starts_with`, `dict.has` | | meets a broken invariant (a bug) | panics | an index out of range, `result.expect` on an `Error` | | leaves the decision to a handler chosen by the caller | an effect operation | a `fail` effect, as in the tutorial | **Rules that follow:** - **The error type says why.** Libraries that only report a message use `String`, as `std/net` and `std/bytes` already did. Code whose callers branch on the reason defines a sum type (the demo's `RegisterError` and `LoginError`). `()` as an error type is avoided: it gives the caller nothing to log. - **Callers choose between `Option` and `Result`.** `option.to_result(o, reason)` and `result.to_option(r)` convert in both directions, so a function keeps the type that describes it. - **Chaining is done with `use`, not new syntax.** `use v <- result.try(r)` stops at the first `Error`, as Gleam does. A `?` operator was considered and rejected: `use` already covers it, works for any type, and keeps one way to write it. - **Raw FFI bindings stay raw.** Generated bindings return what the C function returns (`sortkit.sk_vec_push -> Bool`). The Louise-facing wrapper turns status codes into `Result` (`qsort.sort`). - **Private externs may still return tuples** (`string.parse_int -> (Bool, Int)`, `os.env_raw`): the public function converts them. Changing the extern convention is not needed for this. ### The library `std/result` (Gleam's names, except where Louise already had a Rust name): | Function | | |---|---| | `is_ok`, `is_error` | predicates | | `map`, `map_error`, `replace_error` | change one side | | `try` | continue on `Ok`; for `use` | | `flatten` | `Result, e>` to `Result` | | `unwrap_or`, `unwrap_or_else` | the value, or a default (computed from the reason with `unwrap_or_else`) | | `expect(r, message)` | the value, or a panic with `message` and the reason (`Debug`); for results that are `Ok` unless there is a bug | | `to_option` | drop the reason | | `all`, `values` | lists of results: the values or the first `Error`; the values of the `Ok`s | There is no panicking `unwrap` without a message. `expect` makes the caller say why the result cannot be an error, and the panic message carries it. `std/option` gets the matching pieces: `is_none`, `then` (continue on `Some`, for `use`), `to_result`, and `values`. The prelude adds `Ord>` (`Ok` before `Error`, then the payloads, as a derived `Ord` would) and `Hash>`. There is no `Default` for `Result`: neither side is a natural default. ### API changes | Before | After | |---|---| | `string.to_int(s) -> Option` | `-> Result`; the error is `not an integer: ""` | | `argon2.verify(password, encoded) -> Bool` | `-> Result`: `Ok(False)` on a mismatch, an `Error` when `encoded` cannot be checked | | `qsort.sort(xs, compare) -> List` (panicked when out of memory) | `-> Result, String>` | | demo: the `users` effect's `add_user(user) -> Bool` | `-> Result<(), RegisterError>` (`Error(NameTaken)`) | | demo: `PORT` or `MAX_CONNECTIONS` that is not a number was silently replaced by the default | `main` prints why and stops | | `actor.call` only, which panics without a reply | new `actor.try_call(to, request, timeout) -> Result` (`Error(Timeout)`); `call` stays, built on it, for an actor that must answer | ## Consequences - Callers of `string.to_int` and `argon2.verify` must change: a `Some`/`None` match becomes `Ok`/`Error`, and `if argon2.verify(...)` becomes a `match`. Every caller in this repository is updated. - A login now tells a wrong password apart from a corrupted stored hash: the demo answers the first with 401 and logs the second with 500. - `result.all` and `result.values` are not tail-recursive, like `list.map`: they use stack in proportion to the list. ## Left open - `actor.try_call` reports only `Timeout`. Telling a stopped actor apart (Gleam's `CalleeDown`) needs a monitor per call, and the process API has no `demonitor` to remove it afterwards, so each call would leave one behind. For the same reason, a reply that arrives after the timeout stays in the caller's mailbox until the caller stops. Both need a way to close a subject or remove a monitor in the runtime and the interpreter. *Note — 2026-11-01:* `process.demonitor` exists since #414 (ADR 0004, *Note — 2026-11-01*), in the runtime and the interpreter, so the reason given here no longer holds: `try_call` could monitor the callee and `demonitor_flush` it after the reply or the timeout, and report `CalleeDown`. It still reports only `Timeout`; that change, and closing a subject so that a late reply is dropped, remain open. - Some settings use `-1` for "no limit" (`http.Settings.connections`, receive timeouts). That is absence, not failure, so it is outside this ADR; `Option` would make it explicit.