option
The option module provides functions for working with Option[T] values. It is auto-imported — no import statement needed.
Option[T] represents a value that may or may not exist: some(value) or none. There is no null in Almide.
Constructors
Section titled “Constructors”some(42) // Option[Int] containing 42none // Option[T] with no valuePattern Matching
Section titled “Pattern Matching”The most common way to work with Options:
match opt { some(x) => println("got: " + int.to_string(x)), none => println("nothing"),}Functions
Section titled “Functions”option.map(o: Option[A], f: Fn(A) -> B) -> Option[B]
Section titled “option.map(o: Option[A], f: Fn(A) -> B) -> Option[B]”Transform the inner value if present.
option.map(some(3), (x) => x * 2) // => some(6)option.map(none, (x) => x * 2) // => noneoption.flat_map(o: Option[A], f: Fn(A) -> Option[B]) -> Option[B]
Section titled “option.flat_map(o: Option[A], f: Fn(A) -> Option[B]) -> Option[B]”Transform and flatten. Useful for chaining optional lookups.
option.flat_map(some("42"), (s) => int.parse(s) |> result.to_option)// => some(42)option.flatten(o: Option[Option[A]]) -> Option[A]
Section titled “option.flatten(o: Option[Option[A]]) -> Option[A]”Unwrap a nested Option.
option.flatten(some(some(1))) // => some(1)option.flatten(some(none)) // => noneoption.flatten(none) // => noneoption.unwrap_or(o: Option[A], default: A) -> A
Section titled “option.unwrap_or(o: Option[A], default: A) -> A”Get the inner value, or return a default.
option.unwrap_or(some(42), 0) // => 42option.unwrap_or(none, 0) // => 0option.unwrap_or_else(o: Option[A], f: Fn() -> A) -> A
Section titled “option.unwrap_or_else(o: Option[A], f: Fn() -> A) -> A”Get the inner value, or compute a default lazily.
option.unwrap_or_else(none, () => expensive_default())option.is_some(o: Option[A]) -> Bool
Section titled “option.is_some(o: Option[A]) -> Bool”Check if the option contains a value.
option.is_some(some(1)) // => trueoption.is_some(none) // => falseoption.is_none(o: Option[A]) -> Bool
Section titled “option.is_none(o: Option[A]) -> Bool”Check if the option is empty.
option.is_none(none) // => trueoption.is_none(some(1)) // => falseoption.to_result(o: Option[A], message: String) -> Result[A, String]
Section titled “option.to_result(o: Option[A], message: String) -> Result[A, String]”Convert an Option to a Result, using the message as the error.
option.to_result(some(42), "not found") // => ok(42)option.to_result(none, "not found") // => err("not found")option.filter(o: Option[A], f: Fn(A) -> Bool) -> Option[A]
Section titled “option.filter(o: Option[A], f: Fn(A) -> Bool) -> Option[A]”Keep the value only if it satisfies the predicate.
option.filter(some(3), (x) => x > 2) // => some(3)option.filter(some(1), (x) => x > 2) // => noneoption.zip(a: Option[A], b: Option[B]) -> Option[(A, B)]
Section titled “option.zip(a: Option[A], b: Option[B]) -> Option[(A, B)]”Combine two Options into a pair. Returns none if either is none.
option.zip(some(1), some("a")) // => some((1, "a"))option.zip(some(1), none) // => noneoption.or_else(o: Option[A], f: Fn() -> Option[A]) -> Option[A]
Section titled “option.or_else(o: Option[A], f: Fn() -> Option[A]) -> Option[A]”Return the option if it has a value, otherwise compute an alternative.
option.or_else(none, () => some(42)) // => some(42)option.or_else(some(1), () => some(42)) // => some(1)option.to_list(o: Option[A]) -> List[A]
Section titled “option.to_list(o: Option[A]) -> List[A]”Convert an Option to a single-element or empty list.
option.to_list(some(42)) // => [42]option.to_list(none) // => []Complete Function Reference
Section titled “Complete Function Reference”| Function | Signature | Description |
|---|---|---|
map | (Option[A], Fn(A) -> B) -> Option[B] | Transform inner value |
flat_map | (Option[A], Fn(A) -> Option[B]) -> Option[B] | Transform and flatten |
flatten | (Option[Option[A]]) -> Option[A] | Unwrap nested Option |
unwrap_or | (Option[A], A) -> A | Get value or default |
unwrap_or_else | (Option[A], Fn() -> A) -> A | Get value or compute default |
is_some | (Option[A]) -> Bool | Has value check |
is_none | (Option[A]) -> Bool | Empty check |
to_result | (Option[A], String) -> Result[A, String] | Convert to Result |
filter | (Option[A], Fn(A) -> Bool) -> Option[A] | Conditional keep |
zip | (Option[A], Option[B]) -> Option[(A, B)] | Combine two Options |
or_else | (Option[A], Fn() -> Option[A]) -> Option[A] | Fallback Option |
to_list | (Option[A]) -> List[A] | Convert to List |
Common Patterns
Section titled “Common Patterns”Guard with Option
Section titled “Guard with Option”effect fn process(name: Option[String]) -> Result[Unit, String] = { let n = option.to_result(name, "name required") println("Hello, " + n) ok(())}Chaining lookups
Section titled “Chaining lookups”let result = map.get(config, "database") |> option.flat_map(_, (db) => map.get(db, "host")) |> option.unwrap_or(_, "localhost")Filtering and mapping
Section titled “Filtering and mapping”let valid_ids = list.filter_map(inputs, (s) => { let n = int.parse(s) |> result.to_option option.filter(n, (x) => x > 0)})