← Back to the index

Module std/option.vv

The option module provides a way to represent optional values. An option value is either some(value) or none.

Example:

import option from "std/option.vv"

let val = option.some(42)
let empty = option.none

Table of Contents

Exports

let none

Represents the absence of a value.

Example:

let empty = option.none

fun some(value)

Wraps a value in an option.

Example:

let some_val = option.some(42)

fun is_some(v)

Checks if an option contains a value.

Example:

option.is_some(option.some(1)) // true
option.is_some(option.none)    // false

fun is_none(v)

Checks if an option is empty (none).

Example:

option.is_none(option.none)    // true
option.is_none(option.some(1)) // false

fun get_or(v, default_value)

Returns the wrapped value if it exists, otherwise returns the provided default_value.

Example:

option.get_or(option.some(1), 0) // 1
option.get_or(option.none, 0)    // 0

fun map(v, f)

Applies a function to the wrapped value if it exists, returning a new option.

Example:

option.map(option.some(2), fun(x) return x * 2 end) // some(4)
option.map(option.none, fun(x) return x * 2 end)    // none

fun flat_map(v, f)

Applies a function that returns an option to the wrapped value.

Example:

let f = fun(x) return option.some(x + 1) end
option.flat_map(option.some(1), f) // some(2)

fun flat_map_none(v, f)

Applies a function if the option is none. Useful for chaining fallbacks.

Example:

option.flat_map_none(option.none, fun() return option.some(1) end) // some(1)