Reference · Records and errors
unwrap
signature
unwrap(any) -> anyWhat it does
The value itself when it is not an error value, and a runtime error when it is. It
turns an error value into a stop: the request fails with a 500 and the error's
payload in the message. Use it where an error cannot happen in practice and a silent
default would hide a bug; use ?? for a default and ? to pass the error on.
Examples
print(unwrap(int("42")), unwrap("a"), unwrap([1, 2]))let config = unwrap(json.parse(`{"port": 8080}`))print(config.port)output
42 a [1, 2]8080Edge cases
nullis not an error:unwrap(null)isnull.- The payload goes into the message as text: a map payload shows as a map, a
nullpayload asnull. ??afterunwrapcomes too late: the runtime error has already stopped the program. Put??on the value instead.
print(unwrap(null), unwrap(false))print(int("x") ?? 0)output
null false0Errors
An error value is a runtime error, not an error value: the message is unwrap of an error: and the payload.
this one fails
print(unwrap(err("missing")))output
line 1: unwrap of an error: missingthis one fails
print(unwrap(err({ code: 404 })))output
line 1: unwrap of an error: { code: 404 }Chaining
print(["1", "2"] | map(int) | map(unwrap))print(`[1, 2]` | json.parse | unwrap | sum)output
[1, 2]3See also
err, is_err, int, json.parse
Thread
Thread 0 posts
On unwrap(): questions, tips, corrections, and code that uses it.
No posts yet. The first one opens the thread.
Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo