| @@ -0,0 +1,65 @@ |
| 1 | +# ADR 0024: Errors as values |
| 2 | + |
| 3 | +**Status:** Proposed — 2026-10-11 (issue #87); 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. |
| 4 | + |
| 5 | +## Context |
| 6 | +Issue #87 asks for errors as values: a result type you `match` on, `Ok` or `Error`. The prelude already had `Result<a, e> { Ok(a) Error(e) }`, and `std/result` had `map`, `try` (for `use`) and `unwrap_or`. What was missing: |
| 7 | +- **a convention.** The standard library and the packages each signalled failure in their own way. `string.to_int` returned `Option<Int>`, `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. |
| 8 | +- **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. |
| 9 | +- **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. |
| 10 | + |
| 11 | +## Decision |
| 12 | + |
| 13 | +### The convention |
| 14 | +| A function... | returns | Example | |
| 15 | +|---|---|---| |
| 16 | +| can fail, and the caller should handle why | `Result<a, e>` | `string.to_int`, `net.connect`, `argon2.verify` | |
| 17 | +| may find nothing, and nothing is not a failure | `Option<a>` | `dict.get`, `http.header`, `os.env` | |
| 18 | +| answers a yes-or-no question | `Bool` | `string.starts_with`, `dict.has` | |
| 19 | +| meets a broken invariant (a bug) | panics | an index out of range, `result.expect` on an `Error` | |
| 20 | +| leaves the decision to a handler chosen by the caller | an effect operation | a `fail` effect, as in the tutorial | |
| 21 | + |
| 22 | +**Rules that follow:** |
| 23 | +- **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. |
| 24 | +- **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. |
| 25 | +- **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. |
| 26 | +- **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`). |
| 27 | +- **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. |
| 28 | + |
| 29 | +### The library |
| 30 | +`std/result` (Gleam's names, except where Louise already had a Rust name): |
| 31 | + |
| 32 | +| Function | | |
| 33 | +|---|---| |
| 34 | +| `is_ok`, `is_error` | predicates | |
| 35 | +| `map`, `map_error`, `replace_error` | change one side | |
| 36 | +| `try` | continue on `Ok`; for `use` | |
| 37 | +| `flatten` | `Result<Result<a, e>, e>` to `Result<a, e>` | |
| 38 | +| `unwrap_or`, `unwrap_or_else` | the value, or a default (computed from the reason with `unwrap_or_else`) | |
| 39 | +| `expect(r, message)` | the value, or a panic with `message` and the reason (`Debug`); for results that are `Ok` unless there is a bug | |
| 40 | +| `to_option` | drop the reason | |
| 41 | +| `all`, `values` | lists of results: the values or the first `Error`; the values of the `Ok`s | |
| 42 | + |
| 43 | +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. |
| 44 | + |
| 45 | +`std/option` gets the matching pieces: `is_none`, `then` (continue on `Some`, for `use`), `to_result`, and `values`. |
| 46 | + |
| 47 | +The prelude adds `Ord<Result<a, e>>` (`Ok` before `Error`, then the payloads, as a derived `Ord` would) and `Hash<Result<a, e>>`. There is no `Default` for `Result`: neither side is a natural default. |
| 48 | + |
| 49 | +### API changes |
| 50 | +| Before | After | |
| 51 | +|---|---| |
| 52 | +| `string.to_int(s) -> Option<Int>` | `-> Result<Int, String>`; the error is `not an integer: "<s>"` | |
| 53 | +| `argon2.verify(password, encoded) -> Bool` | `-> Result<Bool, String>`: `Ok(False)` on a mismatch, an `Error` when `encoded` cannot be checked | |
| 54 | +| `qsort.sort(xs, compare) -> List<Int>` (panicked when out of memory) | `-> Result<List<Int>, String>` | |
| 55 | +| demo: the `users` effect's `add_user(user) -> Bool` | `-> Result<(), RegisterError>` (`Error(NameTaken)`) | |
| 56 | +| demo: `PORT` or `MAX_CONNECTIONS` that is not a number was silently replaced by the default | `main` prints why and stops | |
| 57 | + |
| 58 | +## Consequences |
| 59 | +- 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. |
| 60 | +- 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. |
| 61 | +- `result.all` and `result.values` are not tail-recursive, like `list.map`: they use stack in proportion to the list. |
| 62 | + |
| 63 | +## Left open |
| 64 | +- `actor.call` panics when no reply comes within the timeout, as Gleam's `process.call` does. A `try_call` returning a `Result` could follow if a caller needs to recover. |
| 65 | +- Some settings use `-1` for "no limit" (`http.Settings.connections`, receive timeouts). That is absence, not failure, so it is outside this ADR; `Option<Int>` would make it explicit. |