Mojo struct
Optional
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 CollectionElement
so we can implement
copy/move for Optional and allow it to be used in collections itself.
from collections import Optional
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 2
from collections import Optional
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 2
Parametersβ
- βT (
CollectionElement
): The type of value stored in the Optional.
Implemented traitsβ
AnyType
,
Boolable
,
CollectionElement
,
CollectionElementNew
,
Copyable
,
ExplicitlyCopyable
,
Movable
Methodsβ
__init__
β
__init__(inout self: Self)
Construct an empty Optional.
__init__(inout self: Self, owned value: T)
Construct an Optional containing a value.
Args:
- βvalue (
T
): The value to store in the optional.
__init__(inout self: Self, value: None)
Construct an empty Optional.
Args:
- βvalue (
None
): Must be exactlyNone
.
__init__(inout self: Self, value: NoneType)
Construct an empty Optional.
Args:
- βvalue (
NoneType
): Must be exactlyNone
.
__init__(inout self: Self, *, other: Self)
Copy construct an Optional.
Args:
- βother (
Self
): The Optional to copy.
__copyinit__
β
__copyinit__(inout self: Self, other: Self)
Copy construct an Optional.
Args:
- βother (
Self
): The Optional to copy.
__moveinit__
β
__moveinit__(inout self: Self, owned other: Self)
Move this Optional
.
Args:
- βother (
Self
): TheOptional
to move from.
__bool__
β
__bool__(self: Self) -> Bool
Return true if the Optional has a value.
Returns:
True if the optional has a value and False otherwise.
__invert__
β
__invert__(self: Self) -> Bool
Return False if the optional has a value.
Returns:
False if the optional has a value and True otherwise.
__eq__
β
__eq__(self: Self, rhs: NoneType) -> Bool
Return True
if a value is not present.
Args:
- βrhs (
NoneType
): TheNone
value to compare to.
Returns:
True
if a value is not present, False
otherwise.
__eq__[T: EqualityComparableCollectionElement](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 (
EqualityComparableCollectionElement
): The type of the elements in the list. Must implement the traitsCollectionElement
andEqualityComparable
.
Args:
- βrhs (
Optional[T]
): The value to compare to.
Returns:
True if the values are the same.
__ne__
β
__ne__(self: Self, rhs: NoneType) -> Bool
Return True
if a value is present.
Args:
- βrhs (
NoneType
): TheNone
value to compare to.
Returns:
False
if a value is not present, True
otherwise.
__ne__[T: EqualityComparableCollectionElement](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 (
EqualityComparableCollectionElement
): The type of the elements in the list. Must implement the traitsCollectionElement
andEqualityComparable
.
Args:
- βrhs (
Optional[T]
): The value to compare to.
Returns:
False if the values are the same.
__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.
__str__
β
__str__[U: RepresentableCollectionElement, //](self: Optional[U]) -> String
Return the string representation of the value of the Optional.
Parameters:
- βU (
RepresentableCollectionElement
): The type of the elements in the list. Must implement the traitsRepresentable
andCollectionElement
.
Returns:
A string representation of the Optional.
__repr__
β
__repr__[U: RepresentableCollectionElement, //](self: Optional[U]) -> String
Returns the verbose string representation of the Optional.
Parameters:
- βU (
RepresentableCollectionElement
): The type of the elements in the list. Must implement the traitsRepresentable
andCollectionElement
.
Returns:
A verbose string representation of the Optional.
format_to
β
format_to[U: RepresentableCollectionElement, //](self: Optional[U], inout writer: Formatter)
Write Optional string representation to a Formatter
.
Parameters:
- βU (
RepresentableCollectionElement
): The type of the elements in the list. Must implement the traitsRepresentable
andCollectionElement
.
Args:
- βwriter (
Formatter
): The formatter to write to.
value
β
value(ref [self_is_lifetime] self: Self) -> ref [_] T
Retrieve a reference to the value of the Optional.
This check to see if the optional contains a value.
If you call this without first verifying the optional with bool()
eg. by if my_option:
or without otherwise knowing that it contains a
value (for instance with or_else
), the program will abort
Returns:
A reference to the contained data of the option as a Reference[T].
unsafe_value
β
unsafe_value(ref [self_is_lifetime] self: Self) -> ref [_] T
Unsafely retrieve a reference to the value of the Optional.
This doesn't check to see if the optional contains a value.
If you call this without first verifying the optional with bool()
eg. by if my_option:
or without otherwise knowing that it contains a
value (for instance with or_else
), you'll get garbage unsafe data out.
Returns:
A reference to the contained data of the option as a Reference[T].
take
β
take(inout self: Self) -> T
Move the value out of the Optional.
The caller takes ownership over the new value, which is moved out of the Optional, and the Optional is left in an empty state.
This check to see if the optional contains a value.
If you call this without first verifying the optional with bool()
eg. by if my_option:
or without otherwise knowing that it contains a
value (for instance with or_else
), you'll get garbage unsafe data out.
Returns:
The contained data of the option as an owned T value.
unsafe_take
β
unsafe_take(inout self: Self) -> T
Unsafely move the value out of the Optional.
The caller takes ownership over the new value, which is moved out of the Optional, and the Optional is left in an empty state.
This check to see if the optional contains a value.
If you call this without first verifying the optional with bool()
eg. by if my_option:
or without otherwise knowing that it contains a
value (for instance with or_else
), the program will abort!
Returns:
The contained data of the option as an owned T 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?