Silk

silk/option

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 the structural union of Some and None. 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 as Option

fn double(value: i32) -> i32 {
  return value * 2
}

fn positive(value: i32) -> Option.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.

Public declarations: 8.

Some

pub struct Some<T>

The present member of Option, carrying the available owned value.

None

pub struct None

The absent member of Option; it carries no explanation for the absence.

Option

pub struct 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

pub fn none<T>() -> Option<T>

Constructs an absent optional value of the requested element type.

some

pub fn some<T>(value: T) -> Option<T>

Constructs a present option by moving value into it.

map

pub fn map<T, U>(self: Option<T>, transform: once fn(T) -> U) -> 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.

flatMap

pub fn flatMap<T, U>(self: Option<T>, transform: once fn(T) -> Option<U>) -> 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.

unwrapOr

pub fn unwrapOr<T>(self: Option<T>, fallback: T) -> T

Returns 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 as 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: T

The owned alternative consumed only when self is absent.

On this page