Skip to main content
Log in

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__(inout self: Self)

Create an optional with a value of None.

__init__(inout self: Self, value: T)

Create an optional with a value.

Args:

  • ​value (T): The value.

__init__(inout self: Self, value: None)

Construct an empty Optional.

Args:

  • ​value (None): Must be exactly None.

__init__(inout self: Self, value: NoneType)

Create an optional without a value from a None literal.

Args:

  • ​value (NoneType): The None value.

__bool__​

__bool__(self: Self) -> Bool

Return true if the optional has a value.

Returns:

True if the optional has a value and False otherwise.

__is__​

__is__(self: 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: 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: Self) -> T

Get the optional value.

Returns:

The contained value.

or_else​

or_else(self: 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.

Was this page helpful?