std.option.Option
type pub inline enum Option[T]An optional value.
An Option is is either a Some containing a value, or a None that doesn't
contain a value.
Constructors
None
None()The lack of a value.
Some
Some(T)A value of type T.
Instance methods
!=
Show source codeHide source code
fn pub !=(other: ref Self) -> Bool {
!(self == other)
}fn pub !=(other: ref Self) -> Bool
if
T: EqualReturns true if self and the given object are not equal to each other.
==
Show source codeHide source code
fn pub ==(other: ref Option[T]) -> Bool {
match (self, other) {
case (Some(a), Some(b)) -> a == b
case (None, None) -> true
case _ -> false
}
}fn pub ==(other: ref Option[T]) -> Bool
if
T: EqualReturns true if self and the given Option are equal.
Two options are considered equal to each other if:
- They are both None
2. They are both some, and the wrapped values are equal to each other
Examples
Comparing two Some values:
Option.Some(10) == Option.Some(10) # => true
Option.Some(10) == Option.Some(20) # => false
Comparing a Some and a None:
Option.Some(10) == Option.None # => false
Comparing two None values:
Option.None == Option.None # => true
as_mut
Show source codeHide source code
fn pub inline mut as_mut -> Option[mut T] {
match self {
case Some(v) -> Option.Some(v)
case None -> Option.None
}
}fn pub inline mut as_mut -> Option[mut T: mut]
if
T: mutReturns an optional mutable reference to the wrapped value.
Examples
Option.Some([10]).as_mut # => Option.Some(mut [10])
as_ref
Show source codeHide source code
fn pub inline as_ref -> Option[ref T] {
match self {
case Some(v) -> Option.Some(v)
case None -> Option.None
}
}fn pub inline as_ref -> Option[ref T]Returns an optional immutable reference to the wrapped value.
Examples
Option.Some(10).as_ref # => Option.Some(ref 10)
Option.None.as_ref # => Option.None
clone
Show source codeHide source code
fn pub inline clone -> Option[move T] {
match self {
case Some(v) -> Option.Some(v.clone)
case None -> Option.None
}
}fn pub inline clone -> Option[move T]
if
T: CloneCreates a clone of self.
The returned value is an owned value that is the same type as the receiver
of this method. For example, cloning a ref Array[Int] results in a
Array[Int], not another ref Array[Int].
else
Show source codeHide source code
fn pub inline move else(block: fn -> Option[T]) -> Option[T] {
if some? { self } else { block.call }
}fn pub inline move else(block: fn -> Option[T]) -> Option[T]Calls the supplied block if self is a None, returning the Option provided
by the block argument.
If self is a Some, the Some itself is returned.
fmt
Show source codeHide source code
fn pub fmt(formatter: mut Formatter) {
match self {
case Some(v) -> formatter.tuple('Some').field(v).finish
case None -> formatter.tuple('None').finish
}
}fn pub fmt(formatter: mut Formatter)
if
T: FormatFormats self in a human-readable format for debugging purposes.
get
Show source codeHide source code
fn pub move get -> T {
or_panic_with('Option.get expects a Some(_), but a None is found')
}fn pub move get -> TReturns the value wrapped by Some, or panics if self is a None.
Examples
Option.Some(10).get # => 10
map
Show source codeHide source code
fn pub inline move map[R](block: fn (T) -> R) -> Option[R] {
match self {
case Some(v) -> Option.Some(block.call(v))
case None -> Option.None
}
}fn pub inline move map[R](block: fn (T) -> R) -> Option[R]Maps an Option[T] to an Option[R] by wrapping the value returned by
the provided closure.
The closure is to return a value to be wrapped in a Some. If used on a
None, None itself is returned.
Examples
Mapping an Option to a new Option:
Option.Some(10).map fn (num) { num * 2 } # => Option.Some(20)
Mapping a None:
Option.None.map fn (x) { x * 2 } # => Option.None
none?
Show source codeHide source code
fn pub inline none? -> Bool {
match self {
case Some(_) -> false
case None -> true
}
}fn pub inline none? -> BoolReturns true for a None, false otherwise.
Examples
Option.Some(10).none? # => false
Option.None.none? # => true
ok_or
Show source codeHide source code
fn pub inline move ok_or[E](error: E) -> Result[T, E] {
match self {
case Some(v) -> Result.Ok(v)
case _ -> Result.Error(error)
}
}fn pub inline move ok_or[E](error: E) -> Result[T, E]Transforms self into a Result[T, E], mapping an Option.Some(T) to
Result.Ok(T) and a Option.None to Result.Error(E).
The argument is eagerly evaluated. If this isn't desired, use
Option.ok_or_else instead.
Examples
Option.Some(10).ok_or('oops!') # => Result.Ok(10)
Option.None.ok_or('oops!') # => Result.Error('oops!')
ok_or_else
Show source codeHide source code
fn pub inline move ok_or_else[E](error: fn -> E) -> Result[T, E] {
match self {
case Some(v) -> Result.Ok(v)
case _ -> Result.Error(error.call)
}
}fn pub inline move ok_or_else[E](error: fn -> E) -> Result[T, E]Transforms self into a Result[T, E], mapping an Option.Some(T) to
Result.Ok(T) and a Option.None to Result.Error(E) where E is the
return value of the given closure.
Examples
Option.Some(10).ok_or_else(fn { 'oops!' }) # => Result.Ok(10)
Option.None.ok_or_else(fn { 'oops!' }) # => Result.Error('oops!')
or
Show source codeHide source code
fn pub inline move or(default: T) -> T {
match self {
case Some(v) -> v
case None -> default
}
}fn pub inline move or(default: T) -> TReturns the value wrapped by Some, or returns default if self is a
None.
Examples
Option.Some(10).or(0) # => 10
Option.None.or(0) # => 0
or_else
Show source codeHide source code
fn pub inline move or_else(block: fn -> T) -> T {
match self {
case Some(v) -> v
case None -> block.call
}
}fn pub inline move or_else(block: fn -> T) -> TReturns the value wrapped by Some, or returns the closure's return value
if self is a None.
Examples
Option.Some(10).or_else fn { 0 } # => 10
Option.None.or_else fn { 0 } # => 0
or_panic_with
Show source codeHide source code
fn pub inline move or_panic_with(message: String) -> T {
match self {
case Some(v) -> v
case _ -> panic(message)
}
}fn pub inline move or_panic_with(message: String) -> TReturns the value wrapped by Some, or panics with the given message if
self is a None.
Examples
Option.Some(10).or_panic_with('a number must be present') # => 10
some?
Show source codeHide source code
fn pub inline some? -> Bool {
match self {
case Some(_) -> true
case None -> false
}
}fn pub inline some? -> BoolReturns true for a Some, false otherwise.
Examples
Option.Some(10).some? # => true
Option.None.some? # => false
then
Show source codeHide source code
fn pub inline move then[R](block: fn (T) -> Option[R]) -> Option[R] {
match self {
case Some(v) -> block.call(v)
case None -> Option.None
}
}fn pub inline move then[R](block: fn (T) -> Option[R]) -> Option[R]Maps an Option[T] to an Option[R] using the Option returned by the
provided closure.
The closure is to return a new Option. If used on a None, None itself
is returned.
This process is sometimes referred to as a "flat map". Inko uses the name "then" because this reads more nicely when chaining multiple instances of this method together.
zip
Show source codeHide source code
fn pub move zip[O](other: Option[O]) -> Option[(T, O)] {
match self {
case Some(ours) -> {
match other {
case Some(theirs) -> Option.Some((ours, theirs))
case _ -> Option.None
}
}
case _ -> Option.None
}
}fn pub move zip[O](other: Option[O]) -> Option[(T, O)]Zips self with another Option.
If both Option values are a Some, this method returns a Some
containing a tuple of both values. If either is a None, None is
returned.
Implemented traits
Clone
impl Clone for Option
if
T: CloneEqual
impl Equal for Option
if
T: EqualFormat
impl Format for Option
if
T: Format