Esc
Start typing to search...

Result Module

Functions for working with Result values — composable error handling.

Result represents a value that is either successful (Ok value) or an error (Err error). The Result module provides pipe-friendly functions for transforming, chaining, and extracting Result values without explicit pattern matching.

Common patterns

import Result

-- Transform the success value
Ok 5 |> Result.map (|x| x * 2)           -- Ok 10
Err "fail" |> Result.map (|x| x * 2)     -- Err "fail"

-- Chain operations that can fail
let safeDivide = |x| if x == 0 then Err "div by zero" else Ok (100 / x)
Ok 5 |> Result.andThen safeDivide         -- Ok 20
Ok 0 |> Result.andThen safeDivide         -- Err "div by zero"

-- Extract with a default
Ok 42 |> Result.withDefault 0              -- 42
Err "oops" |> Result.withDefault 0         -- 0

-- Convert between Result and Maybe
Result.toMaybe (Ok 5)                      -- Just 5
Result.toMaybe (Err "x")                   -- Nothing
Result.fromMaybe "missing" (Just 5)        -- Ok 5
Result.fromMaybe "missing" Nothing         -- Err "missing"

Result vs Maybe

Result carries an error value explaining why something failed. Maybe only signals whether a value exists, with no reason attached.

ResultMaybe
SuccessOk valueJust value
FailureErr reasonNothing
Error infoYes — the Err holds a valueNo — just absent

Use Result when the caller needs to know what went wrong (parsing, validation, I/O). Use Maybe when absence is routine and needs no explanation (lookups, optional fields). Convert between them with Result.toMaybe (discards the error) and Result.fromMaybe (supplies one).

Functions

Transforming

Result.map

(a -> b) -> Result a e -> Result b e

Transform the Ok value of a Result. If the Result is Err, it is passed through unchanged.

Example:
import Result

(Ok 5) |> Result.map (|x| x + 1)
Try it

See also: Result.mapError, Result.andThen

Result.mapError

(e -> f) -> Result a e -> Result a f

Transform the Err value of a Result. If the Result is Ok, it is passed through unchanged.

Example:
import Result

(Err "not found") |> Result.mapError (|e| "Error: " ++ e)
Try it

See also: Result.map

Chaining

Result.andThen

(a -> Result b e) -> Result a e -> Result b e

Chain a Result-producing function. If the Result is Ok, apply the function to the value. If Err, pass through.

Example:
import Result

(Ok 5) |> Result.andThen (|x| if x > 0 then Ok (x * 2) else Err "negative")
Try it

Notes: Useful for chaining operations that may fail.

See also: Result.map

Extracting

Result.withDefault

a -> Result a e -> a

Extract the Ok value, or use the provided default if the Result is Err.

Example:
import Result

(Err "oops") |> Result.withDefault 0
Try it

See also: Result.map

Converting

Result.toMaybe

Result a e -> Maybe a

Convert a Result to a Maybe. Ok becomes Just, Err becomes Nothing.

Example:
import Result

Result.toMaybe Ok 5

-- Just 5
Try it

See also: Result.fromMaybe

Result.fromMaybe

e -> Maybe a -> Result a e

Convert a Maybe to a Result. Just becomes Ok, Nothing becomes Err with the provided error value.

Example:
import Result

Result.fromMaybe "missing" Just 5

-- Ok 5
Try it

See also: Result.toMaybe