Search results

There are no results.

std.option.Option

class pub 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

Some

Some(T)

A value of type T.

None

None()

The lack of a value.

Instance methods

!=

Show source code
Hide source code
fn pub !=(other: T) -> Bool {
  (self == other).false?
}
fn pub !=(other: T) -> Bool

Returns true if self and the given object are not equal to each other.

==

Show source code
Hide source code
fn pub ==(other: ref Option[T]) -> Bool {
  match self {
    case Some(ours) -> {
      match other {
        case Some(theirs) -> ours == theirs
        case _ -> false
      }
    }
    case None -> {
      match other {
        case None -> true
        case _ -> false
      }
    }
  }
}
fn pub ==(other: ref Option[T]) -> Bool
if
  T: Equal[ref T]

Returns true if self and the given Option are equal.

Two options are considered equal to each other if:

  1. 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 code
Hide source code
fn pub mut as_mut -> Option[mut T] {
  match self {
    case Some(v) -> Option.Some(v)
    case None -> Option.None
  }
}
fn pub mut as_mut -> Option[mut T: mut]
if
  T: mut

Returns an optional mutable reference to the wrapped value.

Examples

Option.Some([10]).as_mut # => Option.Some(mut [10])

as_ref

Show source code
Hide source code
fn pub as_ref -> Option[ref T] {
  match self {
    case Some(v) -> Option.Some(v)
    case None -> Option.None
  }
}
fn pub 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 code
Hide source code
fn pub clone -> Option[T] {
  match self {
    case Some(v) -> Option.Some(v.clone)
    case None -> Option.None
  }
}
fn pub clone -> Option[T]
if
  T: Clone[T]

Creates a clone of self.

else

Show source code
Hide source code
fn pub move else(block: fn -> Option[T]) -> Option[T] {
  if some? { self } else { block.call }
}
fn pub 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 code
Hide 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: Format

Formats self in a human-readable format for debugging purposes.

get

Show source code
Hide source code
fn pub move get -> T {
  or_panic('Option.get expects a Some(_), but a None is found')
}
fn pub move get -> T

Returns the value wrapped by Some, or panics if self is a None.

Examples

Option.Some(10).get # => 10

map

Show source code
Hide source code
fn pub 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 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 code
Hide source code
fn pub none? -> Bool {
  match self {
    case Some(_) -> false
    case None -> true
  }
}
fn pub none? -> Bool

Returns true for a None, false otherwise.

Examples

Option.Some(10).none? # => false
Option.None.none?     # => true

ok_or

Show source code
Hide source code
fn pub move ok_or[E](error: E) -> Result[T, E] {
  match self {
    case Some(v) -> Result.Ok(v)
    case _ -> Result.Error(error)
  }
}
fn pub 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 code
Hide source code
fn pub 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 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 code
Hide source code
fn pub move or(default: T) -> T {
  match self {
    case Some(v) -> v
    case None -> default
  }
}
fn pub move or(default: T) -> T

Returns 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 code
Hide source code
fn pub move or_else(block: fn -> T) -> T {
  match self {
    case Some(v) -> v
    case None -> block.call
  }
}
fn pub move or_else(block: fn -> T) -> T

Returns 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

Show source code
Hide source code
fn pub move or_panic(message: String) -> T {
  match self {
    case Some(v) -> v
    case _ -> panic(message)
  }
}
fn pub move or_panic(message: String) -> T

Returns the value wrapped by Some, or panics with the given message if self is a None.

Examples

Option.Some(10).or_panic('a number must be present') # => 10

some?

Show source code
Hide source code
fn pub some? -> Bool {
  match self {
    case Some(_) -> true
    case None -> false
  }
}
fn pub some? -> Bool

Returns true for a Some, false otherwise.

Examples

Option.Some(10).some? # => true
Option.None.some?     # => false

then

Show source code
Hide source code
fn pub move then[R](block: fn (T) -> Option[R]) -> Option[R] {
  match self {
    case Some(v) -> block.call(v)
    case None -> Option.None
  }
}
fn pub 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 code
Hide 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

std.clone.

Clone

impl Clone[Option[T]] for Option
if
  T: Clone[T]
std.cmp.

Equal

impl Equal[ref Option[T]] for Option
if
  T: Equal[ref T]
std.fmt.

Format

impl Format for Option
if
  T: Format