Mojo struct
Optional
struct Optional[T: Copyable & Movable]
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 Copyable & Movable so we can implement
copy/move for Optional and allow it to be used in collections itself.
Examples:
var a = Optional(1)
var b = Optional[Int](None)
if a:
print(a.value()) # prints 1
if b: # Bool(b) is False, so no print
print(b.value())
var c = a.or_else(2)
var d = b.or_else(2)
print(c) # prints 1
print(d) # prints 2Parameters
Implemented traits
AnyType,
Boolable,
Copyable,
Defaultable,
ImplicitlyCopyable,
Movable,
UnknownDestructibility
Aliases
__copyinit__is_trivial
alias __copyinit__is_trivial = Variant[_NoneType, T].__copyinit__is_trivial
__del__is_trivial
alias __del__is_trivial = Variant[_NoneType, T].__del__is_trivial
__moveinit__is_trivial
alias __moveinit__is_trivial = Variant[_NoneType, T].__moveinit__is_trivial
Methods
__init__
__init__(out self)
Construct an empty Optional.
@implicit
__init__(out self, var value: T)
Construct an Optional containing a value.
Args:
- value (
T): The value to store in theOptional.
@implicit
__init__(out self, value: NoneType)
Construct an empty Optional.
Args:
- value (
NoneType): Must be exactlyNone.
__bool__
__bool__(self) -> Bool
Return true if the Optional has a value.
Returns:
Bool: True if the Optional has a value and False otherwise.
__getitem__
__getitem__(ref self) -> ref [__origin_of($1._value)] T
Retrieve a reference to the value inside the Optional.
Returns:
ref: A reference to the value inside the Optional.
Raises:
On empty Optional.
__invert__
__invert__(self) -> Bool
Return False if the Optional has a value.
Returns:
Bool: False if the Optional has a value and True otherwise.
__eq__
__eq__(self, rhs: NoneType) -> Bool
Return True if a value is not present.
Args:
- rhs (
NoneType): TheNonevalue to compare to.
Returns:
Bool: True if a value is not present, False otherwise.
__eq__[T: EqualityComparable & Copyable & Movable](self: Optional[T], rhs: Optional[T]) -> Bool
Return True if this is the same as another Optional value, meaning both are absent, or both are present and have the same underlying value.
Parameters:
- T (
EqualityComparable&Copyable&Movable): The type of the elements in the list. Must implement the traitsCopyable,MovableandEqualityComparable.
Args:
- rhs (
Optional): The value to compare to.
Returns:
Bool: True if the values are the same.
__ne__
__ne__(self, rhs: NoneType) -> Bool
Return True if a value is present.
Args:
- rhs (
NoneType): TheNonevalue to compare to.
Returns:
Bool: False if a value is not present, True otherwise.
__ne__[T: EqualityComparable & Copyable & Movable, //](self: Optional[T], rhs: Optional[T]) -> Bool
Return False if this is the same as another Optional value, meaning both are absent, or both are present and have the same underlying value.
Parameters:
- T (
EqualityComparable&Copyable&Movable): The type of the elements in the list. Must implement the traitsCopyable,MovableandEqualityComparable.
Args:
- rhs (
Optional): The value to compare to.
Returns:
Bool: False if the values are the same.
__is__
__is__(self, other: NoneType) -> Bool
Return True if the Optional has no value.
Notes:
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.
Notes:
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.
__str__
__str__[U: Copyable & Movable & Representable, //](self: Optional[U]) -> String
Return the string representation of the value of the Optional.
Parameters:
- U (
Copyable&Movable&Representable): The type of the elements in the list. Must implement the traitsRepresentable,CopyableandMovable.
Returns:
String: A string representation of the Optional.
__repr__
__repr__[U: Representable & Copyable & Movable, //](self: Optional[U]) -> String
Returns the verbose string representation of the Optional.
Parameters:
- U (
Representable&Copyable&Movable): The type of the elements in the list. Must implement the traitsRepresentable,CopyableandMovable.
Returns:
String: A verbose string representation of the Optional.
__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.
write_to
write_to[U: Representable & Copyable & Movable, //](self: Optional[U], mut writer: T)
Write Optional string representation to a Writer.
Parameters:
- U (
Representable&Copyable&Movable): The type of the elements in the list. Must implement the traitsRepresentable,CopyableandMovable.
Args:
- writer (
T): The object to write to.
value
value(ref self) -> ref [__origin_of($1._value)] T
Retrieve a reference to the value of the Optional.
Notes:
This will abort on empty Optional.
Returns:
ref: A reference to the contained data of the Optional as a reference.
unsafe_value
unsafe_value(ref self) -> ref [__origin_of($1._value)] T
Unsafely retrieve a reference to the value of the Optional.
Notes:
This will not abort on empty Optional.
Returns:
ref: A reference to the contained data of the Optional as a reference.
take
take(mut self) -> T
Move the value out of the Optional.
Notes:
This will abort on empty Optional.
Returns:
T: The contained data of the Optional as an owned T value.
unsafe_take
unsafe_take(mut self) -> T
Unsafely move the value out of the Optional.
Notes:
This will not abort on empty Optional.
Returns:
T: The contained data of the Optional as an owned T 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:
T: The underlying value contained in the Optional or a default value.
copied
copied[mut: Bool, origin: Origin[mut], //, T: Copyable & Movable](self: Optional[Pointer[T, origin]]) -> Optional[T]
Converts an Optional containing a Pointer to an Optional of an owned value by copying.
Examples:
Copy the value of an Optional[Pointer[_]]
var data = "foo"
var opt = Optional(Pointer(to=data))
var opt_owned: Optional[String] = opt.copied()Notes:
If self is an empty Optional, the returned Optional will be
empty as well.
Parameters:
- mut (
Bool): Mutability of the pointee origin. - origin (
Origin): Origin of the containedPointer. - T (
Copyable&Movable): Type of the owned result value.
Returns:
Optional: An Optional containing an owned copy of the pointee value.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!