On this page
std/options
Source EditThis module implements types which encapsulate an optional value.
A value of type Option[T] either contains a value x (represented as some(x)) or is empty (none(T)).
This can be useful when you have a value that can be present or not. The absence of a value is often represented by nil, but that is not always available, nor is it always a good solution.
Basic usage
Let's start with an example: a procedure that finds the index of a character in a string.
Example:
import std/options
proc find(haystack: string, needle: char): Option[int] =
for i, c in haystack:
if c == needle:
return some(i)
return none(int) # This line is actually optional,
# because the default is empty
let found = "abc".find('c')
assert found.isSome and found.get() == 2The get operation demonstrated above returns the underlying value, or raises UnpackDefect if there is no value. Note that UnpackDefect inherits from system.Defect and should therefore never be caught. Instead, rely on checking if the option contains a value with the isSome and isNone procs.
Pattern matching
fusion/matching supports pattern matching on Options, with the Some(<pattern>) and None() patterns.
{.experimental: "caseStmtMacros".}
import fusion/matching
case some(42)
of Some(@a):
assert a == 42
of None():
assert false
assertMatch(some(some(none(int))), Some(Some(None())))
Imports
Types
Procs
-
proc filter[T](self: Option[T]; callback: proc (input: T): bool): Option[T] {. inline, effectsOf: callback.} -
Applies a
callbackto the value of theOption.If the
callbackreturnstrue, the option is returned assome. If it returnsfalse, it is returned asnone.See also:
Example:
Source Editproc isEven(x: int): bool = x mod 2 == 0 assert some(42).filter(isEven) == some(42) assert none(int).filter(isEven) == none(int) assert some(-11).filter(isEven) == none(int) -
proc flatMap[T, R](self: Option[T]; callback: proc (input: T): Option[R]): Option[ R] {.inline, effectsOf: callback.} -
Applies a
callbackfunction to the value of theOptionand returns the new value.If the
Optionhas no value,none(R)will be returned.This is similar to
map, with the difference that thecallbackreturns anOption, not a raw value. This allows multiple procs with a signature ofA -> Option[B]to be chained together.See also:
Example:
Source Editproc doublePositives(x: int): Option[int] = if x > 0: some(2 * x) else: none(int) assert some(42).flatMap(doublePositives) == some(84) assert none(int).flatMap(doublePositives) == none(int) assert some(-11).flatMap(doublePositives) == none(int) -
proc get[T](self: Option[T]): lent T {.inline.} -
Returns the content of an
Option. If it has no value, anUnpackDefectexception is raised.See also:
- get proc with a default return value
Example:
Source Editassert some(42).get == 42 doAssertRaises(UnpackDefect): echo none(string).get -
proc map[T, R](self: Option[T]; callback: proc (input: T): R): Option[R] {. inline, effectsOf: callback.} -
Applies a
callbackfunction to the value of theOptionand returns anOptioncontaining the new value.If the
Optionhas no value,none(R)will be returned.See also:
- map proc
- flatMap proc for a version with a callback that returns an
Option
Example:
Source Editproc isEven(x: int): bool = x mod 2 == 0 assert some(42).map(isEven) == some(true) assert none(int).map(isEven) == none(bool) -
proc map[T](self: Option[T]; callback: proc (input: T)) {.inline, effectsOf: callback.} -
Applies a
callbackfunction to the value of theOption, if it has one.See also:
- map proc for a version with a callback which returns a value
Example:
Source Editvar d = 0 proc saveDouble(x: int) = d = 2 * x none(int).map(saveDouble) assert d == 0 some(42).map(saveDouble) assert d == 84 -
proc option[T](val: sink T): Option[T] {.inline.} -
Can be used to convert a pointer type (
ptr,pointer,reforproc) to an option type. It convertsniltonone(T). WhenTis no pointer type, this is equivalent tosome(val).See also:
Example:
Source Edittype Foo = ref object a: int b: string assert option[Foo](nil).isNone assert option(42).isSome
© 2006–2024 Andreas Rumpf
Licensed under the MIT License.
https://nim-lang.org/docs/options.html