Option Type
Option[T] represents a value that may or may not exist. It replaces null in other languages.
Definition
type Option[T] =
| Some(T)
| NoneCreating Options
let some_value: Option[i32] = Some(42)
let no_value: Option[i32] = NoneWorking with Options
Pattern Matching
fn describe(opt: Option[i32]) -> string {
var result: string = ""
match opt {
Some(n) => { result = "Has value: " + to_string(n) }
None => { result = "Empty" }
}
return result
}Null Coalesce Operator
Use ?? to provide a default value:
let value: i32 = maybe_value ?? 0
let name: string = maybe_name ?? "Anonymous"Standard Library Functions
The std.option module provides utilities for working with options:
import std.option
// Check if a value exists
let has_value: bool = std.option.is_some(opt)
let is_empty: bool = std.option.is_none(opt)See Standard Library Overview for more details.