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 exactlyNone
.
__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?
Thank you! We'll create more content like this.
Thank you for helping us improve!
π What went wrong?