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/option.md | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 docs/std/option.md (limited to 'docs/std/option.md') diff --git a/docs/std/option.md b/docs/std/option.md new file mode 100644 index 0000000..a048b00 --- /dev/null +++ b/docs/std/option.md @@ -0,0 +1,51 @@ +# Standard Library: Option (`std/option.zc`) + +`Option` represents an optional value: every `Option` is either `Some` and contains a value, or `None`, and does not. + +## Usage + +```zc +import "std/option.zc" + +fn main() { + var val = Option::Some(10); + + if (val.is_some()) { + println "{val.unwrap()}"; + } + + var nothing = Option::None(); + println "{nothing.unwrap_or(0)}"; // Prints 0 +} +``` + +## Structure + +```zc +struct Option { + is_some: bool; + val: T; +} +``` + +## Methods + +### Construction + +| Method | Signature | Description | +| :--- | :--- | :--- | +| **Some** | `Option::Some(v: T) -> Option` | Creates a `Some` option containing `v`. | +| **None** | `Option::None() -> Option` | Creates a `None` option. | + +### Query / Extraction + +| Method | Signature | Description | +| :--- | :--- | :--- | +| **is_some** | `is_some(self) -> bool` | Returns `true` if the option is a `Some` value. | +| **is_none** | `is_none(self) -> bool` | Returns `true` if the option is a `None` value. | +| **unwrap** | `unwrap(self) -> T` | Returns the contained value. Panics if `None`. | +| **unwrap_ref** | `unwrap_ref(self) -> T*` | Returns a pointer to the contained value. Panics if `None`. | +| **unwrap_or** | `unwrap_or(self, def: T) -> T` | Returns the contained value or `def` if `None`. | +| **expect** | `expect(self, msg: char*) -> T` | Returns the contained value or panics with `msg` if `None`. | +| **or_else** | `or_else(self, other: Option) -> Option` | Returns the option if it contains a value, otherwise returns `other`. | +| **forget** | `forget(self)` | Zeroes out memory without destructors (if applicable). | -- cgit v1.2.3