std.result
Unpacking Result(T, E) and accessing fields on the Error payload. Result itself is a record type exported by std.result (RFC-010): construction uses variant constructor syntax, destructuring uses match variant patterns, and ? propagation is driven by the Try interface (see below).
use std.result
r = Result(Int, String).ok(5)
e = Result(Int, String).err("boom")This module is the merge surface for the “dual implementations”: the
Resulttype and the fourTrymethods come from the pure yx implementation insrc/std/result.yx, whileis_ok/is_err/unwrap/unwrap_or/unwrap_err/code/message/errorcome from the native implementation insrc/std/result.rs. Both are registered under the same export surface (src/frontend/module/registry.rs:315-334explicitly performs a union merge, with yx taking precedence for name conflicts), souse std.resultprovides all 9 bindings at once. However, module-levelTrymethods are not visible—result.is_failure(...)reportsE1042; the fourTrymethods can only be accessed through method-call syntax, as inr.is_failure().
Runtime representation
| Value | Representation |
|---|---|
Result(T, E).ok(value) | Enum variant carrying value |
Result(T, E).err(error) | Enum variant carrying error |
Error | Struct with the fields (code, message) |
Error.code is the registered code in RFC-013's E6xxx / E7xxx range (a stable contract across versions), while Error.message is a human-readable description.
Try interface (? propagation)
Result instantiates the four-method interface Try(Result(T, E), T, E) in its type body (src/std/result.yx:22), and the ? operator is driven accordingly. These methods can also be called explicitly, but only through method syntax (calling result.is_failure(...) at module level reports E1042):
| Method | Signature |
|---|---|
r.is_failure() | (T: Type, E: Type)(self: &Result(T, E)) -> Bool |
r.success() | (T: Type, E: Type)(self: &Result(T, E)) -> T |
r.residual() | (T: Type, E: Type)(self: &Result(T, E)) -> E |
Result(T, E).from_error(e) | (T: Type, E: Type)(e: E) -> Result(T, E) |
Unlike std.option, ? propagation on Result is available:
use std.result
use std.string
parse_then_add_one: (String) -> Result(Int, Error) = (s) => {
n = string.parse_int(s)? // Err propagated unchanged
return Result(Int, Error).ok(n + 1)
}
main: () -> Void = {
println(result.unwrap(parse_then_add_one("41"))) // 42
}There is no
map/map_err: these two names are absent from this module's 8 native exports (src/std/result.rs:71-126), soresult.map(...)reportsE1042 field 'map' not found in struct 'result'. To transform the success value, usematchvariant destructuring.
Function overview
| Function | Signature |
|---|---|
is_ok | (T: Type, E: Type)(self: &Result(T, E)) -> Bool |
is_err | (T: Type, E: Type)(self: &Result(T, E)) -> Bool |
unwrap | (T: Type, E: Type)(self: &Result(T, E)) -> T |
unwrap_or | (T: Type, E: Type)(self: &Result(T, E), default: T) -> T |
unwrap_err | (T: Type, E: Type)(self: &Result(T, E)) -> E |
code | (self: &Error) -> String |
message | (self: &Error) -> String |
error | (code: &String, message: &String) -> Error |
Predicates
is_ok
is_ok: (T: Type, E: Type)(self: &Result(T, E)) -> BoolDetermines whether this is the success variant. This is a read-only borrow, so self can be reused repeatedly.
use std.assert
use std.result
main: () -> Void = {
r = Result(Int, String).ok(1)
assert(result.is_ok(r))
assert(result.is_ok(r)) // Can be reused
}is_err
is_err: (T: Type, E: Type)(self: &Result(T, E)) -> BoolDetermines whether this is the error variant. This is a read-only borrow.
use std.assert
use std.result
main: () -> Void = {
r = Result(Int, String).err("e")
assert(result.is_err(r))
}Value extraction
unwrap
unwrap: (T: Type, E: Type)(self: &Result(T, E)) -> TExtracts the success value.
Returns: The value carried by the Ok variant. Errors: Calling it on an Err value throws E6007; the message also includes the original error code and description, in a form such as unwrap called on Err value (E6010: parse_int: ...), so the failure reason can be seen without first calling unwrap_err.
use std.assert
use std.result
use std.string
main: () -> Void = {
r = string.parse_int("42")
assert(result.unwrap(r) == 42)
}unwrap_or
unwrap_or: (T: Type, E: Type)(self: &Result(T, E), default: T) -> TExtracts the success value, or returns default for Err.
default— The fallback value used forErr
use std.assert
use std.result
use std.string
main: () -> Void = {
good = string.parse_int("42")
assert(result.unwrap_or(good, 0) == 42)
bad = string.parse_int("abc")
assert(result.unwrap_or(bad, 0) == 0)
}unwrap_err
unwrap_err: (T: Type, E: Type)(self: &Result(T, E)) -> EExtracts the error value.
Returns: The value carried by the Err variant. Errors: Calling it on an Ok value throws E6007.
use std.assert
use std.result
use std.string
main: () -> Void = {
r = string.parse_int("abc")
e = result.unwrap_err(r)
assert(result.is_err(r))
}Error fields
code
code: (self: &Error) -> StringReads an error code string, such as "E6010".
The signature type is
Error, but the runtime error payload is a struct with the fields(code, message). Call this directly on anErrorvalue.
use std.assert
use std.result
use std.string
main: () -> Void = {
r = string.parse_int("abc")
e = result.unwrap_err(r)
assert(result.code(e) == "E6010")
}message
message: (self: &Error) -> StringReads the error description text.
use std.assert
use std.result
use std.string
main: () -> Void = {
r = string.parse_int("abc")
e = result.unwrap_err(r)
assert(string.len(result.message(e)) > 0)
}Construction
error
error: (code: &String, message: &String) -> ErrorConstructs an Error value. This is the only channel for constructing an Error at the pure yx layer—the Error type family registers only its type identity and has no constructor in the value space, so writing Error("E…", msg) in yx reports E3006 at the IR level (src/std/result.rs:117-124).
code— The error code string (a registered code in RFC-013'sE6xxx/E7xxxrange)message— A human-readable description
Returns: A newly constructed Error value that can be passed directly to code / message for reading, or used as the payload for Result.err(...).
The code table is registry-based:
RUNTIME_ERROR_CODESinsrc/std/result.rs:26-32lists only 5 registered codes (E6009/E6010/E6011/E6012/E6013). A custom code (such as"E9999") can circulate within a program, butyx explaincannot find documentation for it—use it only within a subdomain defined by your own conventions.
use std.assert
use std.result
main: () -> Void = {
e = result.error("E6010", "parse_int failed")
assert(result.code(e) == "E6010")
assert(result.message(e) == "parse_int failed")
r = Result(Int, Error).err(e)
assert(result.is_err(r))
assert(result.code(result.unwrap_err(r)) == "E6010")
}Related
std.string— A parsing function that produces aResultstd.range— Returns anErrcarryingE6009whenstep=0std.option—Option(T); its?propagation is currently unavailable; see “Known gaps” on that page- Error code reference — Runtime error value codes such as
E6010/E6011
