Skip to reference

std.option

wolf-std d58f9e1 · toolchain pin verified against wolf 0.2.41 (0261597). Source signatures and documentation; execution support is stated in each module.

All modules · Plain text · Stability tier: core

Module

std.option — the absence convention over !T rows, and the six helpers that name its commonest shapes. Stability tier: core.

There is no ?T and no null (D30): absence is the row tag none — fn find(…) -> int ! {none} — consumed by else (default and |pat| forms) and propagated by ?. Domain-specific absence tags (eof, gone — weak.upgrade() already returns T ! {gone} per [mem.shared.rc.3]) are legal where the noun earns it; none is the default. API-CONVENTIONS §2 is the binding statement; this module is its worked home.

THE SIX LAND HERE (sc06), five sprints after they were designed. They were blocked from sc01 to sc05 by one grammar fact: a !-row parsed only in RETURN position, so no helper could name its own argument (F-0002, then its interpreter half F-0023). lupin 0.1.3 landed rows in parameter and let positions and 0.1.4 executes them, so every signature this header used to carry as a comment is a body below. Each is thin over else by design — a reader learns the language form first, and these are names for the four commonest shapes plus the two that ask a question about absence:

- or(v, default) ≡ v else default - to_list(v) ≡ 0 or 1 elements - exists(v) ≡ else |_| marking - is_none(v) ≡ else |_| marking, negated - expect(v, msg) ≡ v else |_| { … trap … } — written, executing, and homed in std/x/option_expect (F-0040: a diverging handler needs a bottom type wolf does not spell) - flatten(v) ≡ collapsing (T ! {none}) ! {none} — written, executing, and homed in std/x/option_flatten (F-0039: wolfc's grammar admits one ! per type)

The two nursery residents are not a hedge: each is a complete body with a test, refused by ONE implementation for a reason with an issue number, and one rejected body poisons a module for every importer — so keeping them on this page would cost the other four their clean static ladder. They graduate back here verbatim, in the pin bump's own commit, when their diagnostics stop firing.

Absence has no literal, which is why nothing here carries a fenced example. A row VALUE can only be produced by a function that raises: none resolves at a raise site and nowhere else, so option.or(none, 9) is unsupported: none does not resolve and there is no expression an example line could pass to any function on this page (F-0038). §4's rule then applies exactly as written — unrunnable means prose — and the runnable evidence lives in tests/option/: six entry files, one per helper, each with a local producer and both a hit and a miss.

The lane story: these six are generic over T and take a row, so lupin runs them, the checked tier answers unsupported at the module boundary (F-0015: a raise inside an imported module's item), and the native rung refuses monomorphization. One executing lane is what a generic row helper gets at these pins, and it is the lane that matters here — the bodies are three lines each; what they encode is a convention.

Lazy (closure-taking) variants — or_else and friends — still wait on c05, and Option[T]/Result[T, E] as DATA still wait on s16 plus the s15 try bridge. The recorded decision stands: std wants exactly one of reified rows or generic data, and now that the row helpers execute, the row half is the one carrying its weight.

Types and traits

No public declarations in this section.

Functions and methods

or

fn · Source line 76

pub fn or[T](v: T ! {none}, default: T) -> T

v's value, or default when v is absent. The eager-default form: default is evaluated whether or not it is needed, so keep it cheap (a lazy or_else waits on closures, c05).

Rows: consumes none — the caller receives a T and no row. Total: no traps of its own.

Example (prose — a row value has no literal, F-0038; the runnable form is tests/option/or_else_default.lu): with fn find(x: int) -> int ! {none}, option.or(find(5), 9) is 5 and option.or(find(0), 9) is 9.

to_list

fn · Source line 114

pub fn to_list[T](v: T ! {none}) -> List[T]

v as a list: one element when present, empty when absent. The bridge from absence into the container vocabulary — for x in option.to_list(v) is the loop that runs zero times or once.

Rows: consumes none. Total: no traps. The list is allocated in the ambient region (§5).

Example (prose, F-0038; runnable in tests/option/to_list_roundtrip.lu): option.to_list(find(5)).len is 1 and option.to_list(find(0)).len is 0.

exists

fn · Source line 132

pub fn exists[T](v: T ! {none}) -> bool

True when v is present. This is the question else answers by marking, and answering it CONSUMES the value — a caller that wants the value should reach for or/expect rather than asking first and calling again.

Rows: consumes none. Total: no traps.

Example (prose, F-0038; runnable in tests/option/exists_marking.lu): option.exists(find(5)) is true, option.exists(find(0)) is false.

is_none

fn · Source line 147

pub fn is_none[T](v: T ! {none}) -> bool

True when v is absent — exists's complement, shipped because if option.is_none(v) reads better than if !option.exists(v) at the call sites that ask it that way round. Same consumption caveat.

Rows: consumes none. Total: no traps.

Example (prose, F-0038; runnable in tests/option/is_none_marking.lu): option.is_none(find(0)) is true, option.is_none(find(5)) is false.

Constants

No public declarations in this section.

Errors and row tags

Tags named by public signatures: none. Their conditions are documented on the functions that return them.

No public declarations in this section.