Skip to main content

Mojo struct

OptionalReg

A register-passable optional type.

This struct optionally contains a value. It only works with trivial register passable types at the moment.

Parameters

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

Implemented traits

AnyType, Boolable

Methods

__init__

__init__(out self)

Create an optional with a value of None.

__init__(out self, value: T)

Create an optional with a value.

Args:

  • value (T): The value.

__init__(out self, value: NoneType)

Create an optional without a value from a None literal.

Args:

  • value (NoneType): The None value.

__bool__

__bool__(self) -> Bool

Return true if the optional has a value.

Returns:

True if the optional has a value and False otherwise.

__is__

__is__(self, other: NoneType) -> Bool

Return True if the Optional has no value.

It allows you to use the following syntax: if my_optional is None:

Args:

  • other (NoneType): The value to compare to (None).

Returns:

True if the Optional has no value and False otherwise.

__isnot__

__isnot__(self, other: NoneType) -> Bool

Return True if the Optional has a value.

It allows you to use the following syntax: if my_optional is not None:

Args:

  • other (NoneType): The value to compare to (None).

Returns:

True if the Optional has a value and False otherwise.

value

value(self) -> T

Get the optional value.

Returns:

The contained value.

or_else

or_else(self, default: T) -> T

Return the underlying value contained in the Optional or a default value if the Optional's underlying value is not present.

Args:

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

Returns:

The underlying value contained in the Optional or a default value.