diff options
| author | Zuhaitz Méndez Fernández de Aránguiz <zuhaitz@debian> | 2026-01-23 00:50:18 +0000 |
|---|---|---|
| committer | Zuhaitz Méndez Fernández de Aránguiz <zuhaitz@debian> | 2026-01-23 00:50:18 +0000 |
| commit | 8cb7089b2eb09d40d9497cea40d088d94676a8c6 (patch) | |
| tree | d4a2a33fe35807abc0cdeeb0be93edcbe75a4996 /docs/std/option.md | |
| parent | 3a4a72a38675893c3a1854d05c72b957a6bd9364 (diff) | |
More docs, check 'docs/std'.
Diffstat (limited to 'docs/std/option.md')
| -rw-r--r-- | docs/std/option.md | 51 |
1 files changed, 51 insertions, 0 deletions
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<T>` 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<int>::Some(10); + + if (val.is_some()) { + println "{val.unwrap()}"; + } + + var nothing = Option<int>::None(); + println "{nothing.unwrap_or(0)}"; // Prints 0 +} +``` + +## Structure + +```zc +struct Option<T> { + is_some: bool; + val: T; +} +``` + +## Methods + +### Construction + +| Method | Signature | Description | +| :--- | :--- | :--- | +| **Some** | `Option<T>::Some(v: T) -> Option<T>` | Creates a `Some` option containing `v`. | +| **None** | `Option<T>::None() -> Option<T>` | 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<T>) -> Option<T>` | Returns the option if it contains a value, otherwise returns `other`. | +| **forget** | `forget(self)` | Zeroes out memory without destructors (if applicable). | |
