Mojo struct
OptionalReg
@register_passable(trivial)
struct OptionalReg[T: AnyTrivialRegType]
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,
Copyable,
Defaultable,
DevicePassable,
ImplicitlyCopyable,
Movable,
UnknownDestructibility
Aliases
__copyinit__is_trivial
alias __copyinit__is_trivial = True
__del__is_trivial
alias __del__is_trivial = True
__moveinit__is_trivial
alias __moveinit__is_trivial = True
device_type
alias device_type = OptionalReg[T]
Methods
__init__
__init__() -> Self
Create an optional with a value of None.
@implicit
__init__(value: T) -> Self
Create an optional with a value.
Args:
- value (
T): The value. 
@implicit
__init__(value: NoneType) -> Self
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:
Bool: 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:
Bool: 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:
Bool: True if the Optional has a value and False otherwise.
get_type_name
static get_type_name() -> String
Get the human-readable type name for this OptionalReg type.
Returns:
String: A string representation of the type, e.g. OptionalReg[Int].
get_device_type_name
static get_device_type_name() -> String
Get the human-readable device type name for this OptionalReg type.
Returns:
String: A string representation of the device type (same as type name for OptionalReg).
__merge_with__
__merge_with__[other_type: AnyStruct[Bool]](self) -> Bool
Merge with other bools in an expression.
Parameters:
- other_type (
AnyStruct): The type of the bool to merge with. 
Returns:
Bool: A Bool after merging with the specified other_type.
value
value(self) -> T
Get the optional value.
Returns:
T: The contained value.
or_else
or_else(var self, var 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:
T: 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!