← Back to AILP Home

Result Operators

These operators control how Result<T> values are handled. Every ordinary function call in Nitpick returns Result<T> — these operators let you unwrap, fall back, or discard results explicitly.

Canonical v0.33.6 reference: see guide/result/. This page is kept as a compact operator index.

Operator Table

Operator Keyword Meaning Example
? Result fallback unwrap val = func() ? 0;
?? Optional/pointer null coalesce val = maybe_ptr ?? default;
?! Emphatic Result fallback unwrap val = func() ?! 0;
?\| defaults Scoped fallback for chains val = a() + b() ?| 0;
_? drop Discard Result entirely _? func();
_! raw Extract value, bypass error val = _! func();
contextual catch Error-aware fallback val = func() catch (err) { err + 1; };

Safe Unwrap — ?

Returns the value on success, or the fallback on error:

int32:val = get_number() ? 0;    // 0 if get_number fails

Null Coalesce — ??

string:name = get_name() ?? "unknown";

?? is for Optional/pointer null handling. Result<T> ?? fallback is rejected; use Result<T> ? fallback instead.

Emphatic Unwrap — ?!

?! is Result-only fallback unwrap with emphatic spelling:

int32:val = must_succeed() ?! 0i32;

It does not call failsafe in v0.33.6 source-level semantics.

Defaults — ?| / defaults

Provides a scoped fallback for an entire expression chain:

// Any error in a(), b(), or c() uses 0 as fallback
int32:val = a() + b() + c() defaults 0;

// Shorthand operator form
int32:val = a() + b() + c() ?| 0;

// Nested defaults
int32:val = func(a() + 2 + c(2 + d() defaults 3) defaults 2);

Defaults scopes to the nearest enclosing expression, not the entire statement. The fallback operand is a primary expression in the shipped parser; see guide/result/05-defaults-chains.md.

Contextual catch

catch remains an ordinary identifier except in the exact postfix Result handler shape:

int32:val = risky() catch (err) { err + 10i32 };

The handler sees err:int32 and must yield the same value type as the Result.

Drop — _? / drop

Silently discards the Result (no error check, no value extraction):

drop setup();         // keyword form
_? setup();           // shorthand form

Only works on expressions, NOT on named variables.

Raw — _! / raw

Extracts the value field, ignoring any error:

int32:val = raw risky_func();   // keyword form
int32:val = _! risky_func();    // shorthand form

Not the same as nodrop. raw belongs to the Result system — it unwraps Result<T> into T. nodrop belongs to the RAII system (v0.31.6.x, W-04) — it opts a single binding out of the auto-drop that the IR generator inserts at scope end for wild/wildx/Handle<T>/HandleArena/JIT regions. The two compose: Handle<T>:h = nodrop raw HandleArena.alloc(a, N); unwraps the Result and keeps the explicit-free contract on h. See guide/drop/surface.md for the nodrop surface and guide/drop/regions.md for the per-region rules.

Paren Syntax

All keywords support optional parentheses (v0.4.6+):

drop(func());    // same as: drop func();
raw(func());     // same as: raw func();
pass(value);     // same as: pass value;
fail(code);      // same as: fail code;
exit(0);         // same as: exit 0;

Related