The Option
type is used to represent values that can either be absent or present.
It is defined in the gleam/option
module as follows:
type Option(a) {
Some(a)
None
}
The Some
constructor is used to wrap a value when it's present, and the None
constructor is used to represent the absence of a value.
Accessing the content of a Option
is often done via pattern matching.
import gleam/option.{type Option, None, Some}
pub fn say_hello(person: Option(String)) -> String {
case person {
Some(name) -> "Hello, " <> name <> "!"
None -> "Hello, Friend!"
}
}
say_hello(Some("Matthieu"))
// -> "Hello, Matthieu!"
say_hello(None)
// -> "Hello, Friend!"
The gleam/option
module also defines a number of useful function for working with Option
types, such as unwrap
, which returns the content of an Option
or a default value if it is None
.
import gleam/option.{type Option}
pub fn say_hello_again(person: Option(String)) -> String {
let name = option.unwrap(person, "Friend")
"Hello, " <> name <> "!"
}