Result.unwrap
func Result.unwrap[T, E](r: Result[T, E]): TResult.unwrap returns the value of r. It panics if r is an Err, with a message that shows r's error.
Details
- Use it where an
Erris a bug, such as parsing a constant. For a failure the caller expects, return it withtryor give a fallback with Result.unwrapOr. - In a script, both
tryandResult.unwrapend the script on anErrwith exit status 1:tryreports the error alone, andResult.unwrapreports a panic with its stack trace.
Basic examples
Take the value out of a parsed document:
Result.unwrap(Json.parse("[1, 2]")) // [1.0, 2.0]Call it through . or in a pipe:
Regex.compile("[0-9]+").unwrap() // /[0-9]+/
Json.parse("true") | Result.unwrap() // trueAn Err panics, and the message shows the error:
Result.unwrap(Json.parse("[1,"))
// panic: Result.unwrap on an Err: Error{ message: "unexpected end of JSON input" }Scope
The error type can be anything: a string, a record of your own, or a union:
type NotFound = { key: string }
let missing: Result[int, NotFound] = Result.err(NotFound{ key: "port" })
missing.unwrap() // panic: Result.unwrap on an Err: NotFound{ key: "port" }
let bad: Result[int, string] = Result.err("bad input")
bad.unwrap() // panic: Result.unwrap on an Err: "bad input"A Result[void, Error], such as the one File.write returns, unwraps the same way. On an Ok the call returns void, which carries no information, so there is nothing to use:
File.write("out.txt", "hi").unwrap()
println(File.read("out.txt").unwrap())hiAn Ok may hold null, which Result.unwrap returns like any value:
let found: Result[string | null, Error] = Result.ok(null)
found.unwrap() // nullResult.unwrap reads only the outer tag. On an Ok that holds a Result, it returns the inner Result, which a second call unwraps:
let inner: Result[int, Error] = Result.ok(7)
let outer: Result[Result[int, Error], Error] = Result.ok(inner)
outer.unwrap() // Ok{ value: 7 }
outer.unwrap().unwrap() // 7The value comes back as it is, not copied: an array taken out of a Result is the same array:
let r: Result[Array[int], Error] = Result.ok([1, 2])
let xs = r.unwrap()
xs.push(3)
r // Ok{ value: [1, 2, 3] }Applications
Compile a regular expression written in the source once, at the top of a script. A bad pattern is a bug in the script, so a panic fits:
let digits = Regex.compile("[0-9]+").unwrap()
for line in ["order 66", "no number", "room 101"] {
println(digits.find(line)?.text ?? "-")
}66
-
101Read a configuration written into the program, where a parse error is a mistake of the programmer:
let defaults = Json.parse("{\"port\": 8080, \"debug\": false}").unwrap()
println(defaults)Map{ "port": 8080.0, "debug": false }Parse a fixed list of dates with Result.all, and unwrap once for the whole list:
let holidays = ["2026-01-01", "2026-12-25"]
| Array.map(Time.parsePlainDate)
| Result.all()
| Result.unwrap()
println(holidays.length())2Properties and relations
On an Ok, Result.unwrap returns what Result.unwrapOr returns, whatever the fallback:
let r: Result[int, Error] = Result.ok(5)
assert(r.unwrap() == r.unwrapOr(0))
assert(r.unwrap() == r.unwrapOr(-1))Unwrapping after Result.map is the same as applying the function to the unwrapped value:
let r: Result[string, Error] = Result.ok("ant")
let f = func(s: string): int { s.length() }
assert(r.map(f).unwrap() == f(r.unwrap()))Result.isOk tells in advance whether Result.unwrap panics, which Panic.catch confirms:
let r: Result[int, string] = Result.err("no")
assert(!r.isOk())
assert(!Panic.catch({ r.unwrap() }).isOk())Possible issues
An Err from input the program does not control, such as a file or a user, is not a bug. With Result.unwrap a missing file ends the script with a panic and a stack trace:
let text = File.read("settings.json").unwrap()
println(text)Return the error with try instead, so the script ends with the message alone:
let text = try File.read("settings.json")
println(text)Or give a fallback with Result.unwrapOr:
let text = File.read("settings.json").unwrapOr("{}")
println(text){}Result.unwrap takes a Result, not a Result that may be null. Handle the null first, with ?? and a fallback Result:
let cached: Result[int, Error] | null = null
let n = Result.unwrap(cached)
// error: cannot use Result[int, Error] | null where Result[int, Error] is expectedlet cached: Result[int, Error] | null = null
let fallback: Result[int, Error] = Result.ok(0)
Result.unwrap(cached ?? fallback) // 0See also
Result.unwrapOr · Result.isOk · Result.map · Result.all · try · Panic.catch · Errors guide
