tobogganSe connecter

louise

The Louise language: compiler, runtime, std, tooling

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

67 lignes · 6396 octetsBrut · Historique

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, 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<a, e> { 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<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.
  • a small library. Without map_error, all or a conversion from Option, code fell back to nested matches, 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...returnsExample
can fail, and the caller should handle whyResult<a, e>string.to_int, net.connect, argon2.verify
may find nothing, and nothing is not a failureOption<a>dict.get, http.header, os.env
answers a yes-or-no questionBoolstring.starts_with, dict.has
meets a broken invariant (a bug)panicsan index out of range, result.expect on an Error
leaves the decision to a handler chosen by the calleran effect operationa 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_errorpredicates
map, map_error, replace_errorchange one side
trycontinue on Ok; for use
flattenResult<Result<a, e>, e> to Result<a, e>
unwrap_or, unwrap_or_elsethe 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_optiondrop the reason
all, valueslists of results: the values or the first Error; the values of the Oks

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

API changes

BeforeAfter
string.to_int(s) -> Option<Int>-> Result<Int, String>; the error is not an integer: "<s>"
argon2.verify(password, encoded) -> Bool-> Result<Bool, String>: Ok(False) on a mismatch, an Error when encoded cannot be checked
qsort.sort(xs, compare) -> List<Int> (panicked when out of memory)-> Result<List<Int>, 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 defaultmain prints why and stops
actor.call only, which panics without a replynew actor.try_call(to, request, timeout) -> Result<r, CallError> (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<Int> would make it explicit.