Source Edit

This 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:

  1. import std/options
  2. proc find(haystack: string, needle: char): Option[int] =
  3. for i, c in haystack:
  4. if c == needle:
  5. return some(i)
  6. return none(int) # This line is actually optional,
  7. # because the default is empty
  8. let found = "abc".find('c')
  9. assert found.isSome and found.get() == 2

The 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

Note: This requires the fusion package.

fusion/matching supports pattern matching on Options, with the Some(<pattern>) and None() patterns.

  1. {.experimental: "caseStmtMacros".}
  2. import fusion/matching
  3. case some(42)
  4. of Some(@a):
  5. assert a == 42
  6. of None():
  7. assert false
  8. assertMatch(some(some(none(int))), Some(Some(None())))

Imports

typetraits

Types

  1. Option[T] = object
  2. when T is SomePointer:
  3. else:

An optional type that may or may not contain a value of type T. When T is a a pointer type (ptr, pointer, ref, proc or iterator {.closure.}), none(T) is represented as nil. Source Edit

  1. UnpackDefect = object of Defect

Source Edit

  1. UnpackError {....deprecated: "See corresponding Defect".} = UnpackDefect

Deprecated: See corresponding Defect

Source Edit

Procs

  1. proc `$`[T](self: Option[T]): string

Get the string representation of the Option.

Example:

  1. assert $some(42) == "some(42)"
  2. assert $none(int) == "none(int)"

Source Edit

  1. proc `==`[T](a, b: Option[T]): bool {.inline.}

Returns true if both Options are none, or if they are both some and have equal values.

Example:

  1. let
  2. a = some(42)
  3. b = none(int)
  4. c = some(42)
  5. d = none(int)
  6. assert a == c
  7. assert b == d
  8. assert not (a == b)

Source Edit

  1. proc filter[T](self: Option[T]; callback: proc (input: T): bool): Option[T] {.
  2. inline, effectsOf: callback.}

Applies a callback to the value of the Option.

If the callback returns true, the option is returned as some. If it returns false, it is returned as none.

See also:

Example:

  1. proc isEven(x: int): bool =
  2. x mod 2 == 0
  3. assert some(42).filter(isEven) == some(42)
  4. assert none(int).filter(isEven) == none(int)
  5. assert some(-11).filter(isEven) == none(int)

Source Edit

  1. proc flatMap[T, R](self: Option[T]; callback: proc (input: T): Option[R]): Option[
  2. R] {.inline, effectsOf: callback.}

Applies a callback function to the value of the Option and returns the new value.

If the Option has no value, none(R) will be returned.

This is similar to map, with the difference that the callback returns an Option, not a raw value. This allows multiple procs with a signature of A -> Option[B] to be chained together.

See also:

Example:

  1. proc doublePositives(x: int): Option[int] =
  2. if x > 0:
  3. some(2 * x)
  4. else:
  5. none(int)
  6. assert some(42).flatMap(doublePositives) == some(84)
  7. assert none(int).flatMap(doublePositives) == none(int)
  8. assert some(-11).flatMap(doublePositives) == none(int)

Source Edit

  1. proc flatten[T](self: Option[Option[T]]): Option[T] {.inline.}

Remove one level of structure in a nested Option.

See also:

Example:

  1. assert flatten(some(some(42))) == some(42)
  2. assert flatten(none(Option[int])) == none(int)

Source Edit

  1. proc get[T](self: Option[T]): lent T {.inline.}

Returns the content of an Option. If it has no value, an UnpackDefect exception is raised.

See also:

Example:

  1. assert some(42).get == 42
  2. doAssertRaises(UnpackDefect):
  3. echo none(string).get

Source Edit

  1. proc get[T](self: Option[T]; otherwise: T): T {.inline.}

Returns the content of the Option or otherwise if the Option has no value.

Example:

  1. assert some(42).get(9999) == 42
  2. assert none(int).get(9999) == 9999

Source Edit

  1. proc get[T](self: var Option[T]): var T {.inline.}

Returns the content of the var Option mutably. If it has no value, an UnpackDefect exception is raised.

Example:

  1. var
  2. a = some(42)
  3. b = none(string)
  4. inc(a.get)
  5. assert a.get == 43
  6. doAssertRaises(UnpackDefect):
  7. echo b.get

Source Edit

  1. proc isNone[T](self: Option[T]): bool {.inline.}

Checks if an Option is empty.

See also:

Example:

  1. assert not some(42).isNone
  2. assert none(string).isNone

Source Edit

  1. proc isSome[T](self: Option[T]): bool {.inline.}

Checks if an Option contains a value.

See also:

Example:

  1. assert some(42).isSome
  2. assert not none(string).isSome

Source Edit

  1. proc map[T, R](self: Option[T]; callback: proc (input: T): R): Option[R] {.
  2. inline, effectsOf: callback.}

Applies a callback function to the value of the Option and returns an Option containing the new value.

If the Option has no value, none(R) will be returned.

See also:

Example:

  1. proc isEven(x: int): bool =
  2. x mod 2 == 0
  3. assert some(42).map(isEven) == some(true)
  4. assert none(int).map(isEven) == none(bool)

Source Edit

  1. proc map[T](self: Option[T]; callback: proc (input: T)) {.inline,
  2. effectsOf: callback.}

Applies a callback function to the value of the Option, if it has one.

See also:

  • map proc_2) for a version with a callback which returns a value

Example:

  1. var d = 0
  2. proc saveDouble(x: int) =
  3. d = 2 * x
  4. none(int).map(saveDouble)
  5. assert d == 0
  6. some(42).map(saveDouble)
  7. assert d == 84

Source Edit

  1. proc none(T: typedesc): Option[T] {.inline.}

Returns an Option for this type that has no value.

See also:

Example:

  1. assert none(int).isNone

Source Edit

  1. proc none[T](): Option[T] {.inline.}

Alias for none(T). Source Edit

  1. proc option[T](val: sink T): Option[T] {.inline.}

Can be used to convert a pointer type (ptr, pointer, ref or proc) to an option type. It converts nil to none(T). When T is no pointer type, this is equivalent to some(val).

See also:

Example:

  1. type
  2. Foo = ref object
  3. a: int
  4. b: string
  5. assert option[Foo](nil).isNone
  6. assert option(42).isSome

Source Edit

  1. proc some[T](val: sink T): Option[T] {.inline.}

Returns an Option that has the value val.

See also:

Example:

  1. let a = some("abc")
  2. assert a.isSome
  3. assert a.get == "abc"

Source Edit

  1. proc unsafeGet[T](self: Option[T]): lent T {.inline.}

Returns the value of a some. The behavior is undefined for none.

Note: Use this only when you are absolutely sure the value is present (e.g. after checking with isSome). Generally, using the get proc is preferred.

Source Edit