Search results

There are no results.

std.result.Result

type pub inline enum Result[T, E]

A type that represents either success (Ok(T)) or failure (Error(E)).

Constructors

Error

Error(E)

The case and value for an error.

Ok

Ok(T)

The case and value for a successful result.

Static methods

collect

Show source code
Hide source code
fn pub static collect[I: mut + Iter[Result[T, E]]](
  iter: I,
) -> Result[Array[T], E] {
  let vals = []

  loop {
    match iter.next {
      case Some(Ok(val)) -> vals.push(val)
      case Some(Error(err)) -> throw err
      case _ -> break
    }
  }

  Result.Ok(vals)
}
fn pub static collect[I: mut + Iter[Result[T, E]]](iter: I: mut) -> Result[Array[T], E]

Collects values from an Iter[Result[T, E]] into a Result[Array[T], E], returning the first Error encountered when iterating over the iterator.

Examples

let vals = [Result.Ok(1), Result.Error('oops!'), Result.Ok(2)].into_iter
let result = Result.collect(vals)

result.error? # => true

Instance methods

!=

Show source code
Hide source code
fn pub !=(other: ref Self) -> Bool {
  (self == other).false?
}
fn pub !=(other: ref Self) -> Bool
if
  E: Equal,
  T: Equal

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

==

Show source code
Hide source code
fn pub ==(other: ref Result[T, E]) -> Bool {
  match (self, other) {
    case (Ok(a), Ok(b)) -> a == b
    case (Error(a), Error(b)) -> a == b
    case _ -> false
  }
}
fn pub ==(other: ref Result[T, E]) -> Bool
if
  E: Equal,
  T: Equal

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

This operator is used to perform structural equality. This means two objects residing in different memory locations may be considered equal, provided their structure is equal. For example, two different arrays may be considered to have structural equality if they contain the exact same values.

clone

Show source code
Hide source code
fn pub inline clone -> Result[move T, move E] {
  match self {
    case Ok(v) -> Result.Ok(v.clone)
    case Error(v) -> Result.Error(v.clone)
  }
}
fn pub inline clone -> Result[move T, move E]
if
  E: Clone,
  T: Clone

Creates 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 code
Hide source code
fn pub inline move else[R](block: fn (E) -> Result[T, R]) -> Result[T, R] {
  match self {
    case Ok(v) -> Result.Ok(v)
    case Error(e) -> block.call(e)
  }
}
fn pub inline move else[R](block: fn (E) -> Result[T, R]) -> Result[T, R]

Maps a Result[T, E] into a Result[T, R].

If self is an Error, the supplied closure is called and its returned Result is returned. If self is an Ok, the Ok is returned as-is.

Examples

let foo: Result[Int, String] = Result.Error('oops!')

res.else fn (val) {
  Result.Error(val.to_upper)
}
# => Result.Error('OOPS!')

error

Show source code
Hide source code
fn pub inline move error -> Option[E] {
  match self {
    case Error(v) -> Option.Some(v)
    case _ -> Option.None
  }
}
fn pub inline move error -> Option[E]

Converts self into an Option[E].

If self is an Error, a Some(E) is returned, otherwise a None is returned.

Examples

let res: Result[Int, String] = Result.Error('oops!')

res.error # => Option.Some(42)

error?

Show source code
Hide source code
fn pub inline error? -> Bool {
  match self {
    case Error(_) -> true
    case _ -> false
  }
}
fn pub inline error? -> Bool

Returns true if self is an Err.

Examples

let res: Result[Int, String] = Result.Error('oops!')

res.error? # => true

fmt

Show source code
Hide source code
fn pub fmt(formatter: mut Formatter) {
  match self {
    case Ok(v) -> formatter.tuple('Ok').field(v).finish
    case Error(v) -> formatter.tuple('Error').field(v).finish
  }
}
fn pub fmt(formatter: mut Formatter)
if
  E: Format,
  T: Format

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

get

Show source code
Hide source code
fn pub inline move get -> T {
  or_panic_with('Result.get expects an Ok(_), but an Error(_) is found')
}
fn pub inline move get -> T

Returns the value wrapped by the Ok case, or panics if self is an Error.

Panics

This method panics if self is an Error.

Examples

let res: Result[Int, String] = Result.Ok(42)

res.get # => 42

map

Show source code
Hide source code
fn pub inline move map[R](block: fn (T) -> R) -> Result[R, E] {
  match self {
    case Ok(v) -> Result.Ok(block.call(v))
    case Error(e) -> Result.Error(e)
  }
}
fn pub inline move map[R](block: fn (T) -> R) -> Result[R, E]

Maps a Result[T, E] into a Result[R, E].

If self is an Ok, the supplied closure is called and its value used to return a new Ok. If self is an Error, the Error is returned as-is.

Examples

let foo: Result[Int, String] = Result.Ok(42)

res.map fn (val) { val.to_string } # => Result.Ok('42')

map_error

Show source code
Hide source code
fn pub inline move map_error[R](block: fn (E) -> R) -> Result[T, R] {
  match self {
    case Ok(v) -> Result.Ok(v)
    case Error(e) -> Result.Error(block.call(e))
  }
}
fn pub inline move map_error[R](block: fn (E) -> R) -> Result[T, R]

Maps a Result[T, E] into a Result[T, R].

If self is an Error, the supplied closure is called and its value used to return a new Error. If self is an Ok, the Ok is returned as-is.

Examples

let foo: Result[Int, String] = Result.Error('oops!')

res.map_error fn (val) { val.to_upper } # => Result.Ok('OOPS!')

ok

Show source code
Hide source code
fn pub inline move ok -> Option[T] {
  match self {
    case Ok(v) -> Option.Some(v)
    case _ -> Option.None
  }
}
fn pub inline move ok -> Option[T]

Converts self into an Option[T].

If self is an Ok, a Some(T) is returned, otherwise a None is returned.

Examples

let res: Result[Int, String] = Result.Ok(42)

res.ok # => Option.Some(42)

ok?

Show source code
Hide source code
fn pub inline ok? -> Bool {
  match self {
    case Ok(_) -> true
    case _ -> false
  }
}
fn pub inline ok? -> Bool

Returns true if self is an Ok.

Examples

let res: Result[Int, String] = Result.Ok(42)

res.ok? # => true

or

Show source code
Hide source code
fn pub inline move or(default: T) -> T {
  match self {
    case Ok(val) -> val
    case _ -> default
  }
}
fn pub inline move or(default: T) -> T

Returns the value wrapped by Ok, or returns default if self is an Error.

Examples

let foo: Result[Int, String] = Result.Ok(42)
let bar: Result[Int, String] = Result.Error('oops!')

foo.or(0) # => 42
bar.or(0) # => 0

or_else

Show source code
Hide source code
fn pub inline move or_else(block: fn (E) -> T) -> T {
  match self {
    case Ok(v) -> v
    case Error(v) -> block.call(v)
  }
}
fn pub inline move or_else(block: fn (E) -> T) -> T

Returns the value wrapped by Ok, or returns the closure's return value if self is an Error.

The argument passed to the closure is the value wrapped by the Error case.

Examples

let foo: Result[Int, String] = Result.Ok(42)
let bar: Result[Int, String] = Result.Error('oops!')

foo.or_else fn (_) { 0 } # => 42
bar.or_else fn (_) { 0 } # => 0

or_panic

Show source code
Hide source code
fn pub inline move or_panic -> T {
  match self {
    case Ok(v) -> v
    case Error(e) -> panic(e.to_string)
  }
}
fn pub inline move or_panic -> T
if
  E: ToString

Returns the value wrapped by Ok if self is an Ok, or panics by using the Error value as the panic message.

For this method to be available, the type wrapped by the Error case must implement the std.string.ToString trait.

If you want to use a custom error message, use Result.or_panic_with instead.

Examples

let ok: Result[Int, String] = Result.Ok(10)
let error: Result[Int, String] = Result.Error('oops!')

ok.or_panic    # => 10
error.or_panic # => panic("oops!")

or_panic_with

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

Returns the value wrapped by Ok, or panics with the given message if self is an Error.

Examples

let res: Result[Int, String] = Result.Ok(10)

res.or_panic_with('a number must be present') # => 10

then

Show source code
Hide source code
fn pub inline move then[R](block: fn (T) -> Result[R, E]) -> Result[R, E] {
  match self {
    case Ok(v) -> block.call(v)
    case Error(e) -> Result.Error(e)
  }
}
fn pub inline move then[R](block: fn (T) -> Result[R, E]) -> Result[R, E]

Maps a Result[T, E] into a Result[R, E].

If self is an Ok, the supplied closure is called and its returned Result is returned. If self is an Error, the Error is returned as-is.

Examples

let foo: Result[Int, String] = Result.Ok(42)

res.then fn (val) { Result.Ok(val.to_string) } # => Result.Ok('42')

Implemented traits

std.clone.

Clone

impl Clone for Result
if
  E: Clone,
  T: Clone
std.cmp.

Equal

impl Equal for Result
if
  E: Equal,
  T: Equal
std.fmt.

Format

impl Format for Result
if
  E: Format,
  T: Format