silk/option
Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc.
Optional owned values that distinguish presence from absence without a failure channel.
When to use
Use Option when absence is an expected answer and needs no error payload. Use map for a
pure transform, flatMap when the transform may also return absence, and unwrapOr only
when the caller is ready to consume the option.
Details
Option<T> is a nominal union with Some and None variants. Its combinators preserve affine
ownership: a present value moves forward, while an unused fallback or abandoned branch drops.
Examples
Transform and continue only when a value is present
import silk.option { Option }
fn double(value: i32) -> i32 {
return value * 2
}
fn positive(value: i32) -> Option<i32> {
if value > 0 {
return Option.some<i32>(value)
}
return Option.none<i32>()
}
pub fn main() -> i32 {
let initial = Option.some<i32>(21)
let doubled = Option.map<i32, i32>(move initial, double)
let answer = Option.flatMap<i32, i32>(move doubled, positive)
let absent = Option.none<i32>()
let missing = Option.map<i32, i32>(move absent, double)
let presentValue = Option.unwrapOr<i32>(move answer, 0)
let absentValue = Option.unwrapOr<i32>(move missing, 0)
return presentValue + absentValue
}Import as Option with import silk.option { Option }.
Public declarations: 1.
Option
pub union Option<T>An owned value that is either Some or None.
Details
Match on an Option when both arms need custom behavior. Prefer map, flatMap, or
unwrapOr for the common transform, continue, and default cases.
None
Option<T>.None: Option<T>The absent variant; it carries no explanation for the absence.
Some
Option<T>.Some { value: T }: Option<T>The present variant, carrying the available owned value.
Field value
pub value: TThe value moved through present-only combinator branches.
Associated function Option.none
pub fn none<T>() -> silk/option.Option<T>Constructs an absent optional value of the requested element type.
Associated function Option.some
pub fn some<T>(value: T) -> silk/option.Option<T>Constructs a present option by moving value into it.
Method Option.map
pub fn map<T, U, 'life2>(self: Option<T>, transform: once fn<'life2>(T) -> U) -> silk/option.Option<U>Applies transform once to a present value and keeps an absent value absent.
Details
The callback is not called for None. This operation consumes self; use a shared borrow and
match instead when the original option must remain available.
Method Option.flatMap
pub fn flatMap<T, U, 'life2>(self: Option<T>, transform: once fn<'life2>(T) -> silk/option.Option<U>) -> silk/option.Option<U>Continues a present value with a transform that itself answers with an Option, so the outcome stays one Option deep instead of nesting.
Details
The callback runs once for Some and not at all for None. Use this when the next step may
reject the value without needing to explain why; use a Result when rejection needs an error.
Method Option.unwrapOr
pub fn unwrapOr<T>(self: Option<T>, fallback: T) -> TReturns the present value, or the fallback value when the option is absent.
Details
Only the absent arm consumes the fallback. The present arm releases it, so exactly one of the two owned values leaves this call and the other drops.
Examples
Choose between a present value and a fallback
import silk.option { Option }
pub fn main() -> i32 {
let present = Option.some<i32>(7)
let absent = Option.none<i32>()
let first = move present
|> Option.unwrapOr<i32>(0)
let second = move absent
|> Option.unwrapOr<i32>(5)
return first + second
}Parameter fallback
fallback: TThe owned alternative consumed only when self is absent.