Result
type Result[T, E] = Ok[T] | Err[E]Result is the outcome of an operation that can fail: an Ok with a value or an Err with an error. Test it with is, or use the Result functions.
type Ok[T] = { value: T }Ok is the success case of a Result, holding its value.
type Err[E] = { error: E }Err is the failure case of a Result, holding its error.
A function that can fail in an expected way returns a Result: Ok with its value, or Err with its error. The Result namespace holds the functions that build and take results. Each function that takes a result takes it first, so Result.unwrapOr(r, 0), r.unwrapOr(0), and r | Result.unwrapOr(0) are the same call.
Details
OkandErrare ordinary records.Result.ok(v)is the same value asOk{ value: v }, andResult.err(e)the same asErr{ error: e }.Result.fail(m)isResult.err(Error{ message: m }), for a result whose error type isError.tryunwraps anOk. On anErrit returns anErrwith the same error from the enclosing function, which must return aResultwhose error type accepts it.- The error type is any type: the library's
Error, a string, or a union of records the program declares. - Result.unwrap panics on an
Err, for code where anErris a bug.
Basic examples
Result.ok and Result.err build a result. is tells the two cases apart:
let good: Result[int, string] = Result.ok(8080)
let bad: Result[int, string] = Result.err("not a number")
good // Ok{ value: 8080 }
bad // Err{ error: "not a number" }
good is Ok // true
Result.isOk(bad) // falseAfter is Ok, the value is in .value; in the else branch, the error is in .error:
let r = Json.parse("[1, 2]")
if r is Ok {
println(r.value)
} else {
println("bad input: ${r.error.message}")
}[1.0, 2.0]try unwraps an Ok and returns any Err to the caller:
func parseInt(s: string): Result[int, string] {
let n = s.toInt() ?? return Result.err("not a number: ${s}")
Result.ok(n)
}
func add(a: string, b: string): Result[int, string] {
let x = try parseInt(a)
let y = try parseInt(b)
Result.ok(x + y)
}
add("2", "3") // Ok{ value: 5 }
add("2", "two") // Err{ error: "not a number: two" }Scope
The error type can be a union of records. Narrow the result, then narrow its error:
type Empty = {}
type TooLong = { max: int }
type NameError = Empty | TooLong
func checkName(s: string): Result[string, NameError] {
if s.isEmpty() {
return Result.err(Empty{})
}
if s.length() > 8 {
return Result.err(TooLong{ max: 8 })
}
Result.ok(s)
}
for s in ["ada", "", "bartholomew"] {
let r = checkName(s)
if r is Ok {
println("ok: ${r.value}")
} else {
let e = r.error
if e is Empty {
println("empty name")
} else {
println("longer than ${e.max}")
}
}
}ok: ada
empty name
longer than 8A Result[void, E] reports only success or failure. The standard library's Error is a record with a message:
func save(ok: bool): Result[void, Error] {
if !ok {
return Result.fail("disk full")
}
Result.ok(void)
}
save(true) // Ok{ value: void }
save(false) // Err{ error: Error{ message: "disk full" } }A record literal builds the same value as the constructor:
let a: Result[int, string] = Ok{ value: 1 }
let b: Result[int, string] = Result.ok(1)
a == b // truetry reads only the outer case. On an Ok holding a result, it gives the inner result:
func outer(): Result[Result[int, string], string] {
Result.ok(Result.err("inner"))
}
func run(): Result[int, string] {
let inner = try outer()
println(inner)
Result.ok(0)
}
println(run())Err{ error: "inner" }
Ok{ value: 0 }Functions by job
Building
- Result.ok: an
Okcarrying a value - Result.err: an
Errcarrying an error - Result.fail: an
Errcarrying anErrorwith a message
Reading
- Result.isOk: whether a result is an
Ok - Result.unwrap: the value, or a panic on an
Err - Result.unwrapOr: the value, or a fallback on an
Err
Transforming
- Result.map: apply a function to the value of an
Ok - Result.mapErr: apply a function to the error of an
Err - Result.wrapErr: put context in front of an
Errormessage
Combining
- Result.all:
Okof every value, or the firstErr
Applications
Add context as an error goes up. Under try, parenthesize the call together with its Result.wrapErr stage:
func loadPort(text: string): Result[int, Error] {
let json = try (Json.parse(text) | Result.wrapErr("parse settings"))
if !(json is float64) {
return Result.fail("port is not a number")
}
let port = Math.toInt(json) ?? return Result.fail("port ${json} is not whole")
Result.ok(port)
}
for text in ["8080", "80.5", "{"] {
let r = loadPort(text) | Result.wrapErr("load config")
if r is Ok {
println("port ${r.value}")
} else {
println(r.error.message)
}
}port 8080
load config: port 80.5 is not whole
load config: parse settings: unexpected end of JSON inputIn a script's top-level code, try on an Err ends the script with status 1. This script writes parse settings: unexpected end of JSON input to standard error and prints nothing:
let settings = try (Json.parse("{") | Result.wrapErr("parse settings"))
println(settings)Parse every input, and get the first failure or every value:
func parsePort(s: string): Result[int, string] {
let n = s.toInt() ?? return Result.err("not a number: ${s}")
Result.ok(n)
}
["80", "443"].map(parsePort) | Result.all() // Ok{ value: [80, 443] }
["80", "http"].map(parsePort) | Result.all() // Err{ error: "not a number: http" }Or keep going, with a fallback for each failure:
func parsePort(s: string): Result[int, string] {
let n = s.toInt() ?? return Result.err("not a number: ${s}")
Result.ok(n)
}
["80", "http"].map({ parsePort(x).unwrapOr(0) }) // [80, 0]Properties and relations
Result.isOk(r) is r is Ok, Result.unwrapOr(r, 0) is the value of an Ok or the fallback, and Result.map keeps an Err as it is:
let rs: Array[Result[int, string]] = [Result.ok(2), Result.err("no")]
for r in rs {
assert(Result.isOk(r) == (r is Ok))
assert(Result.unwrapOr(r, 0) == if r is Ok { r.value } else { 0 })
}
let e: Result[int, string] = Result.err("no")
assert(Result.map(e, { x * 2 }) == e)Possible issues
Result.ok alone does not say what the error type is. Annotate the variable:
let r = Result.ok(1) // error: cannot infer the type parameter E of Result.ok; annotate the resultlet r: Result[int, string] = Result.ok(1)
r // Ok{ value: 1 }try needs a function that returns a Result:
func first(text: string): float64 {
let json = try Json.parse(text)
// error: try would return Err[Error], but the function returns float64
0.0
}try binds tighter than |: it unwraps the first stage and pipes the value on. Parenthesize the whole pipeline:
func load(text: string): Result[Json.Value, Error] {
let json = try Json.parse(text) | Result.wrapErr("parse settings")
// error: cannot use Json.Value where Result[T, Error] is expected
Result.ok(json)
}func load(text: string): Result[Json.Value, Error] {
let json = try (Json.parse(text) | Result.wrapErr("parse settings"))
Result.ok(json)
}
load("{").isOk() // falseA result with a narrower error type does not fit where a wider one is expected, because type arguments are invariant. try widens on the way out; elsewhere, widen with Result.mapErr(r, { x }):
type Empty = {}
type TooLong = { max: int }
func check(s: string): Result[string, Empty] {
if s.isEmpty() { Result.err(Empty{}) } else { Result.ok(s) }
}
func validate(s: string): Result[string, Empty | TooLong] {
check(s)
// error: cannot use Result[string, Empty] where Result[string, Empty | TooLong] is expected
}type Empty = {}
type TooLong = { max: int }
func check(s: string): Result[string, Empty] {
if s.isEmpty() { Result.err(Empty{}) } else { Result.ok(s) }
}
func validate(s: string): Result[string, Empty | TooLong] {
Result.mapErr(check(s), { x })
}
validate("") // Err{ error: Empty{} }Result.unwrap on an Err panics. Prefer try, Result.unwrapOr, or is, unless an Err is a bug:
let r: Result[int, string] = Result.err("no")
Result.unwrap(r) // panic: Result.unwrap on an Err: "no"See also
- Error, the library's error type, and Error.wrap
- Panic.catch: turn a panic into an
Err - try, is
- Errors
