Result

steps.result.Result
See theResult companion object
enum Result[+T, +E] extends IterableOnce[T]

Represents either a success value of type T or an error of type E. It can be used when expecting results that have errors that should be inspected by the caller. For this, Result have a precise error type parameter.

To create one, directly use one of the variant constructors Ok or Err, or start a computation scope with Result.apply:

extension[T] (it: IterableOnce[T])
 def tryMap[U, E](f: T => Result[U, E]): Result[Seq[U], E] =
   Result:
     it.iterator.map(f(_).ok) // shorts-circuit on the first Err and returns

Tail-recursive functions can be implemented by using eval.break:

extension[T] (seq: Seq[T])
 @scala.annotation.tailrec
 def tryFoldLeft[U, E](init: U)(f: (U, T) => Result[U, E]) =
   Result:
     seq match
       case Seq() => init
       case Seq(h, t*) => eval.break(t.tryFoldLeft(f(init, h)))

// however, a much simpler implementation is
extension[T] (it: IterableOnce[T])
 def tryFoldLeft[U, E](init: U)(f: (U, T) => Result[U, E]) =
   Result:
     it.iterator.foldLeft(init)(f(_, _).ok)

Conversions from Option and Either are available in ScalaConverters (or implicitly through importing Conversions).

// from Option
val opt: Option[Int] = f()
val res1 = opt.okOr(Error.NotFound) // with explicit error
val res2 = opt.asResult // Result[Int, NoSuchElementException]
// to Option
val opt2 = res1.ok // returns Option[Int]

// from Either
val either: Either[E, T] = f()
val res = either.asResult // Result[T, E]
// to Either
val either2 = res.toEither

// from Try
val t: Try[T] = Try { /* ... */ }
val res = t.asResult // Result[T, Throwable]
// to Try
val t2 = res.toTry // Try[T], if error type is throwable
val t2 = Try { res.get } // Try[T], throws ResultIsErrException

Casual usage in a library where precise error reporting is preferred would consist of creating the Error type as an enum or an union type, aliasing the Result type with the correct error type, and using it as part of the function signature.

enum LibError:
 case A
 case B(inner: SomeError)
// or ...
type LibError = ErrorA.type | SomeError

type LibResult[+T] = Result[T, LibError]

object LibResult:
 import scala.util.boundary
 export Result.{apply as _, *}

 // override `apply` manually, to have it fix the Error type parameter.
 inline def apply[T](inline body: boundary.Label[LibResult[T]] => T) = Result.apply(body)

// in the library:
def ApiEndpoint(p: Int): LibResult[String] =
 LibResult:
   // ...

In end applications, prefer either:

  • directly matching over the Result, where precise errors need to be inspected.
  • in other cases, where tracing is wanted and error details are less important, prefer unwrapping the Result directly and catch the Result.ResultIsErrException at the top level to inspect the error details.

Attributes

Companion
object
Graph
Supertypes
trait Enum
trait Serializable
trait Product
trait Equals
trait IterableOnce[T]
class Object
trait Matchable
class Any
Show all
Known subtypes
class Ok[T]
class Err[E]

Members list

Grouped members

Value accessors and disambiguators

def exists(pred: T => Boolean): Boolean
Extension method from Result

Returns true if result contains an Ok value that satisfies pred.

Returns true if result contains an Ok value that satisfies pred.

Attributes

def forall(pred: T => Boolean): Boolean
Extension method from Result

Returns true if result contains an Ok value that satisfies pred.

Returns true if result contains an Ok value that satisfies pred.

Attributes

def get: T
Extension method from Result

Returns the Ok value from the result, and throws an exception if it is an error.

Returns the Ok value from the result, and throws an exception if it is an error.

Attributes

Throws
ResultIsErrException

if the result is an Err.

def getErr: E
Extension method from Result

Returns the error from the result, and throws an exception if it is an Ok value.

Returns the error from the result, and throws an exception if it is an Ok value.

Attributes

Throws
java.util.NoSuchElementException

if the result is an Ok.

def getNullable: T | Null
Extension method from Result

Unwraps the inner value, returing null if there is an error.

Unwraps the inner value, returing null if there is an error.

Attributes

def getOrElse(default: => T): T
Extension method from Result

Returns the Ok value from the result, or default if the result is an Error.

Returns the Ok value from the result, or default if the result is an Error.

Attributes

def isErr: Boolean
Extension method from Result

Returns whether the result is Err.

Returns whether the result is Err.

Attributes

def isOk: Boolean
Extension method from Result

Returns whether the result is Ok.

Returns whether the result is Ok.

Attributes

def tap(peek: T => Unit): r.type
Extension method from Result

Runs peek with the wrapped Ok value, if it exists.

Runs peek with the wrapped Ok value, if it exists.

Attributes

def tapErr(peek: E => Unit): r.type
Extension method from Result

Runs peek with the wrapped Err error, if it exists.

Runs peek with the wrapped Err error, if it exists.

Attributes

Transformers to different types under Result

def flatMap[U](f: T => Result[U, E]): Result[U, E]
Extension method from Result

Returns the output of f from applying it to the Ok value, otherwise keeping the Err case.

Returns the output of f from applying it to the Ok value, otherwise keeping the Err case.

Attributes

def flatten: Result[T, E]
Extension method from Result

Flattens a nested Result with the same error type.

Flattens a nested Result with the same error type.

Attributes

def handleErr[E1](f: E => Result[T, E1]): Result[T, E1]
Extension method from Result

Returns the output of f from applying it to the Err case error, otherwise keeping the Ok case. Similar to flatMap, but on the Err case.

Returns the output of f from applying it to the Err case error, otherwise keeping the Ok case. Similar to flatMap, but on the Err case.

Attributes

def map[U](f: T => U): Result[U, E]
Extension method from Result

Maps the Ok value through f, otherwise keeping the Err error.

Maps the Ok value through f, otherwise keeping the Err error.

Attributes

def mapErr[E1](f: E => E1): Result[T, E1]
Extension method from Result

Maps the Err error through f, otherwise keeping the Ok value.

Maps the Err error through f, otherwise keeping the Ok value.

Attributes

Combining multiple results into a new one

def and[U, E1](other: => Result[U, E1]): Result[(T, U), E | E1]
Extension method from Result

Returns a tuple of r and other if both are Ok, or the first error otherwise. Short-circuits, so other is not evaluated if r is an Err.

Returns a tuple of r and other if both are Ok, or the first error otherwise. Short-circuits, so other is not evaluated if r is an Err.

Attributes

def andTrace[U, E1](other: => Result[U, E1]): Result[(T, U), Either[E, E1]]
Extension method from Result

Returns a tuple of r and other if both are Ok, or the first error otherwise. Short-circuits, so other is not evaluated if r is an Err.

Returns a tuple of r and other if both are Ok, or the first error otherwise. Short-circuits, so other is not evaluated if r is an Err.

Unlike and, returns the error disambiguated by Either: Left if r is an Err, and Right if r is Ok but other is not.

Attributes

infix def cons[Ts <: Tuple](other: Result[Ts, List[E]]): Result[T *: Ts, List[E]]
Extension method from Result

Generalized version of zip to work with arbitrary tuples.

Generalized version of zip to work with arbitrary tuples.

Attributes

See also

zip

def or[E1](other: => Result[T, E1]): Result[T, E1]
Extension method from Result

Returns r if it is Ok, otherwise evaluates and returns other.

Returns r if it is Ok, otherwise evaluates and returns other.

Attributes

def zip[U](other: Result[U, E]): Result[(T, U), List[E]]
Extension method from Result

Returns a tuple of r and other if both are Ok, other return all errors as a List. Does '''not''' short-circuit.

Returns a tuple of r and other if both are Ok, other return all errors as a List. Does '''not''' short-circuit.

Attributes

Conversions into other types

def errOption: Option[E]
Extension method from Result

Returns the Err error from the result.

Returns the Err error from the result.

Attributes

def toEither: Either[E, T]
Extension method from Result

Conversionerts the result into an Either.

Conversionerts the result into an Either.

Attributes

def toOption: Option[T]
Extension method from Result

Converts the result into an Option, with Some case if the value is Ok.

Converts the result into an Option, with Some case if the value is Ok.

Attributes

def toSeq: Seq[T]
Extension method from Result

Returns the Seq that is one element (the Ok value) or otherwise empty.

Returns the Seq that is one element (the Ok value) or otherwise empty.

Attributes

def toTry: Try[T]
Extension method from Result

Conversionerts the result to scala.util.Try.

Conversionerts the result to scala.util.Try.

Attributes

Type members

Enum entries

final case class Err[+E](error: E) extends Result[Nothing, E]

Contains the error value

Contains the error value

Attributes

final case class Ok[+T](value: T) extends Result[T, Nothing]

Contains the success value

Contains the success value

Attributes