Skip to content

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).

yaoxiang
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 Result type and the four Try methods come from the pure yx implementation in src/std/result.yx, while is_ok / is_err / unwrap / unwrap_or / unwrap_err / code / message / error come from the native implementation in src/std/result.rs. Both are registered under the same export surface (src/frontend/module/registry.rs:315-334 explicitly performs a union merge, with yx taking precedence for name conflicts), so use std.result provides all 9 bindings at once. However, module-level Try methods are not visible—result.is_failure(...) reports E1042; the four Try methods can only be accessed through method-call syntax, as in r.is_failure().

Runtime representation ​

ValueRepresentation
Result(T, E).ok(value)Enum variant carrying value
Result(T, E).err(error)Enum variant carrying error
ErrorStruct 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):

MethodSignature
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:

yaoxiang
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), so result.map(...) reports E1042 field 'map' not found in struct 'result'. To transform the success value, use match variant destructuring.

Function overview ​

FunctionSignature
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 ​

yaoxiang
is_ok: (T: Type, E: Type)(self: &Result(T, E)) -> Bool

Determines whether this is the success variant. This is a read-only borrow, so self can be reused repeatedly.

yaoxiang
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 ​

yaoxiang
is_err: (T: Type, E: Type)(self: &Result(T, E)) -> Bool

Determines whether this is the error variant. This is a read-only borrow.

yaoxiang
use std.assert
use std.result

main: () -> Void = {
    r = Result(Int, String).err("e")
    assert(result.is_err(r))
}

Value extraction ​

unwrap ​

yaoxiang
unwrap: (T: Type, E: Type)(self: &Result(T, E)) -> T

Extracts 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.

yaoxiang
use std.assert
use std.result
use std.string

main: () -> Void = {
    r = string.parse_int("42")
    assert(result.unwrap(r) == 42)
}

unwrap_or ​

yaoxiang
unwrap_or: (T: Type, E: Type)(self: &Result(T, E), default: T) -> T

Extracts the success value, or returns default for Err.

  • default — The fallback value used for Err
yaoxiang
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 ​

yaoxiang
unwrap_err: (T: Type, E: Type)(self: &Result(T, E)) -> E

Extracts the error value.

Returns: The value carried by the Err variant. Errors: Calling it on an Ok value throws E6007.

yaoxiang
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 ​

yaoxiang
code: (self: &Error) -> String

Reads 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 an Error value.

yaoxiang
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 ​

yaoxiang
message: (self: &Error) -> String

Reads the error description text.

yaoxiang
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 ​

yaoxiang
error: (code: &String, message: &String) -> Error

Constructs 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's E6xxx / E7xxx range)
  • 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_CODES in src/std/result.rs:26-32 lists only 5 registered codes (E6009 / E6010 / E6011 / E6012 / E6013). A custom code (such as "E9999") can circulate within a program, but yx explain cannot find documentation for it—use it only within a subdomain defined by your own conventions.

yaoxiang
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")
}
  • std.string — A parsing function that produces a Result
  • std.range — Returns an Err carrying E6009 when step=0
  • std.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