From 8cb7089b2eb09d40d9497cea40d088d94676a8c6 Mon Sep 17 00:00:00 2001 From: Zuhaitz Méndez Fernández de Aránguiz Date: Fri, 23 Jan 2026 00:50:18 +0000 Subject: More docs, check 'docs/std'. --- docs/std/result.md | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 docs/std/result.md (limited to 'docs/std/result.md') diff --git a/docs/std/result.md b/docs/std/result.md new file mode 100644 index 0000000..1f9390e --- /dev/null +++ b/docs/std/result.md @@ -0,0 +1,55 @@ +# Standard Library: Result (`std/result.zc`) + +`Result` is the standard type for error handling. It represents either success (`Ok`) containing a value, or failure (`Err`) containing a string error message. + +## Usage + +```zc +import "std/result.zc" + +fn divide(a: int, b: int) -> Result { + if (b == 0) { + return Result::Err("Division by zero"); + } + return Result::Ok(a / b); +} + +fn main() { + var res = divide(10, 2); + if (res.is_ok()) { + println "Result: {res.unwrap()}"; + } else { + println "Error: {res.err}"; + } +} +``` + +## Structure + +```zc +struct Result { + is_ok: bool; + val: T; + err: char*; +} +``` + +## Methods + +### Construction + +| Method | Signature | Description | +| :--- | :--- | :--- | +| **Ok** | `Result::Ok(v: T) -> Result` | Creates a success result containing `v`. | +| **Err** | `Result::Err(e: char*) -> Result` | Creates an error result with message `e`. | + +### Query / Extraction + +| Method | Signature | Description | +| :--- | :--- | :--- | +| **is_ok** | `is_ok(self) -> bool` | Returns `true` if the result is `Ok`. | +| **is_err** | `is_err(self) -> bool` | Returns `true` if the result is `Err`. | +| **unwrap** | `unwrap(self) -> T` | Returns the contained value. Panics if `Err`. | +| **unwrap_ref** | `unwrap_ref(self) -> T*` | Returns a pointer to the contained value. Panics if `Err`. | +| **expect** | `expect(self, msg: char*) -> T` | Returns the contained value or panics with `msg` if `Err`. | +| **forget** | `forget(self)` | Zeroes out memory without destructors. | -- cgit v1.2.3