optional

Module

Defines Optional, a type modeling a value which may or may not be present.

Optional values can be thought of as a type-safe nullable pattern. Your value can take on a value or None, and you need to check and explicitly extract the value to get it out.

from collections.optional import Optional
var a = Optional(1)
var b = Optional[Int](None)
if a:
    print(a.value())  # prints 1
if b:  # b is False, so no print
    print(b.value())
var c = a.or_else(2)
var d = b.or_else(2)
print(c.value())  # prints 1
print(d.value())  # prints 2

Optional

A type modeling a value which may or may not be present.

Optional values can be thought of as a type-safe nullable pattern. Your value can take on a value or None, and you need to check and explicitly extract the value to get it out.

Currently T is required to be a CollectionElement so we can implement copy/move for Optional and allow it to be used in collections itself.

from collections.optional import Optional
var a = Optional(1)
var b = Optional[Int](None)
if a:
    print(a.value())  # prints 1
if b:  # b is False, so no print
    print(b.value())
var c = a.or_else(2)
var d = b.or_else(2)
print(c.value())  # prints 1
print(d.value())  # prints 2

Parameters:

  • T (CollectionElement): The type of value stored in the Optional.

Implemented traits:

AnyType, CollectionElement, Copyable, Movable

Methods:

__init__

__init__(inout self: Self)

Construct an empty Optional.

__init__(inout self: Self, owned value: T)

Construct an Optional containing a value.

Args:

  • value (T): The value to store in the optional.

__init__(inout self: Self, value: None)

Construct an empty Optional.

Args:

  • value (None): Must be exactly None.

__bool__

__bool__(self: Self) -> Bool

Whether or not the Optional contains a value.

Returns:

True if the Optional contains a value, False otherwise.

value

value(self: Self) -> T

Unsafely retrieve the value out of the Optional.

This function currently creates a copy. Once we have lifetimes we’ll be able to have it return a reference.

This doesn’t check to see if the optional contains a value. If you call this without first verifying the optional with bool() eg. by if my_option: or without otherwise knowing that it contains a value (for instance with or_else), you’ll get garbage unsafe data out.

Returns:

The contained data of the option as a T value.

take

take(owned self: Self) -> T

Unsafely move the value out of the Optional.

The caller takes ownership over the new value, and the Optional is destroyed.

This doesn’t check to see if the optional contains a value. If you call this without first verifying the optional with bool() eg. by if my_option: or without otherwise knowing that it contains a value (for instance with or_else), you’ll get garbage unsafe data out.

Returns:

The contained data of the option as an owned T value.

or_else

or_else(self: Self, default: T) -> Self

Make an Optional containing the same value, or a default value if a value wasn’t present.

Args:

  • default (T): The new value to use if no value was present.

Returns:

A new Optional containing the old value or default.