sealed trait Result[+E, +T] extends Any
A Rust Result<T, E>
inspired interface for handling results.
Result
is a type that represents either success (Ok) or failure
(Err). See the package documentation for details.
- Grouped
- Alphabetic
- By Inheritance
- Result
- Any
- Hide All
- Show All
- Public
- Protected
Concrete Value Members
- final def !=(arg0: Any): Boolean
- Definition Classes
- Any
- final def ##: Int
- Definition Classes
- Any
- final def ==(arg0: Any): Boolean
- Definition Classes
- Any
- def and[F >: E, U >: T](rhs: => Result[F, U]): Result[F, U]
Returns
rhs
if the result isOk
, otherwise returns thisErr
value.Returns
rhs
if the result isOk
, otherwise returns thisErr
value.Examples
>>> val x1: Result[String, Int] = Ok(2) >>> val y1: Result[String, String] = Err("late error") >>> x1.and(y1) Err(late error) >>> val x2: Result[String, Int] = Err("early error") >>> val y2: Result[String, String] = Ok("foo") >>> x2.and(y2) Err(early error) >>> val x3: Result[String, Int] = Err("not a 2") >>> val y3: Result[String, String] = Err("late error") >>> x3.and(y3) Err(not a 2) >>> val x4: Result[String, Int] = Ok(2) >>> val y4: Result[String, String] = Ok("different result type") >>> x4.and(y4) Ok(different result type)
- def andThen[F >: E, U](op: (T) => Result[F, U]): Result[F, U]
Calls
op
if theResult
isOk
, otherwise returns thisErr
value.Calls
op
if theResult
isOk
, otherwise returns thisErr
value.This function can be used for control flow based on
Result
values. Often used to chain fallible operations that may returnErr
.An alias of flatMap
Examples
>>> def ensureEven(x: Int): Result[String, Int] = if (x % 2 == 0) Ok(x) else Err("Odd Number") >>> def ensurePositive(x: Int): Result[String, Int] = if (x > 0) Ok(x) else Err("Not Positive") >>> Ok(2).andThen(ensureEven).andThen(ensurePositive) Ok(2) >>> Ok(1).andThen(ensureEven).andThen(ensurePositive) Err(Odd Number) >>> Ok(-2).andThen(ensureEven).andThen(ensurePositive) Err(Not Positive) >>> Err("Some Error").andThen(ensureEven).andThen(ensurePositive) Err(Some Error)
- final def asInstanceOf[T0]: T0
- Definition Classes
- Any
- def contains[U >: T](x: => U): Boolean
Returns
true
if the result is anOk
value containing the given value.Returns
true
if the result is anOk
value containing the given value.Examples
>>> val x: Result[String, Int] = Ok(2) >>> x.contains(2) true >>> val y: Result[String, Int] = Ok(3) >>> y.contains(2) false >>> val z: Result[String, Int] = Err("Some error message") >>> z.contains(2) false
- def containsErr[U >: E](x: => U): Boolean
Returns
true
if the result is anErr
value containing the given value.Returns
true
if the result is anErr
value containing the given value.Examples
>>> val x: Result[String, Int] = Ok(2) >>> x.containsErr("Some error message") false >>> val y: Result[String, Int] = Err("Some error message") >>> y.containsErr("Some error message") true >>> val z: Result[String, Int] = Err("Some other error message") >>> z.containsErr("Some error message") false
- def equals(arg0: Any): Boolean
- Definition Classes
- Any
- final def err: Option[E]
Converts from
Result[E, T]
toOption[E]
.Converts from
Result[E, T]
toOption[E]
.Converts
this
into anOption[E]
, discarding the success value, if any.Examples
>>> val x: Result[String, Int] = Ok(2) >>> x.err == None true >>> val y: Result[String, Int] = Err("Nothing here") >>> y.err == Some("Nothing here") true
- def exists(p: (T) => Boolean): Boolean
Returns
false
ifErr
or returns the result of the application of the given predicate to theOk
value.Returns
false
ifErr
or returns the result of the application of the given predicate to theOk
value.Examples
>>> Ok(12).exists(_ > 10) true >>> Ok(7).exists(_ > 10) false >>> Err[Int, Int](12).exists(_ => true) false
- def existsErr(p: (E) => Boolean): Boolean
Returns
false
ifOk
or returns the result of the application of the given predicate to theErr
value.Returns
false
ifOk
or returns the result of the application of the given predicate to theErr
value.Examples
>>> Err(12).existsErr(_ > 10) true >>> Err(7).existsErr(_ > 10) false >>> Ok[Int, Int](12).existsErr(_ => true) false
- def expect(msg: => String): T
Returns the contained
Ok
value.Returns the contained
Ok
value.Throws
Throws if the value is an
Err
, with a exception message combining the passed message and theErr
's value.Examples
>>> val x: Result[String, Int] = Err("emergency failure") >>> intercept[RuntimeException](x.expect("Testing expect")).getMessage Testing expect: emergency failure
- Annotations
- @throws(classOf[RuntimeException])
- def expectErr(msg: => String): E
Returns the contained
Err
value.Returns the contained
Err
value.Throws
Throws if the value is an
Ok
, with a exception message combining the passed message and theOk
's value.Examples
>>> val x: Result[Int, String] = Ok("unexpected success") >>> intercept[RuntimeException](x.expectErr("Testing expect")).getMessage Testing expect: unexpected success
- def filterErrOrElse[U >: T](p: (E) => Boolean, default: => U): Result[E, U]
Returns the existing
Err
if this is anErr
and the predicatep
holds for the err value, orOk(default)
if the predicatep
does not hold, else returns the existingOk
.Returns the existing
Err
if this is anErr
and the predicatep
holds for the err value, orOk(default)
if the predicatep
does not hold, else returns the existingOk
.Examples
>>> Err(12).filterErrOrElse(_ > 10, -1) Err(12) >>> Err(7).filterErrOrElse(_ > 10, -1) Ok(-1) >>> Ok(7).filterErrOrElse(_ => false, -1) Ok(7)
- def filterOrElse[F >: E](p: (T) => Boolean, default: => F): Result[F, T]
Returns the existing
Ok
if this is anOk
and the predicatep
holds for the ok value, orErr(default)
if the predicatep
does not hold, else returns the existingErr
.Returns the existing
Ok
if this is anOk
and the predicatep
holds for the ok value, orErr(default)
if the predicatep
does not hold, else returns the existingErr
.Examples
>>> Ok(12).filterOrElse(_ > 10, -1) Ok(12) >>> Ok(7).filterOrElse(_ > 10, -1) Err(-1) >>> Err(7).filterOrElse(_ => false, -1) Err(7)
- def flatMap[F >: E, U](op: (T) => Result[F, U]): Result[F, U]
An alias of andThen for compatibility with for-comprehensions and consistency with Scala naming
- def flatMapErr[F, U >: T](op: (E) => Result[F, U]): Result[F, U]
An alias of orElse for consistency with Scala naming (
Err
suffix required for disambiguation) - def flatten[F >: E, U](implicit ev: <:<[T, Result[F, U]]): Result[F, U]
Converts from
Result[E, Result[E, T]]
toResult[E, T]
Converts from
Result[E, Result[E, T]]
toResult[E, T]
Examples
>>> val x: Result[Int, Result[Int, String]] = Ok(Ok("hello")) >>> x.flatten Ok(hello) >>> val y: Result[Int, Result[Int, String]] = Ok(Err(6)) >>> y.flatten Err(6) >>> val z: Result[Int, Result[Int, String]] = Err(6) >>> z.flatten Err(6) // Flattening only removes one level of nesting at a time: >>> val multi: Result[Int, Result[Int, Result[Int, String]]] = Ok(Ok(Ok("hello"))) >>> multi.flatten Ok(Ok(hello)) >>> multi.flatten.flatten Ok(hello)
- def flattenErr[F, U >: T](implicit ev: <:<[E, Result[F, U]]): Result[F, U]
Converts from
Result[E, T, Result[T, E]]
toResult[T]
Converts from
Result[E, T, Result[T, E]]
toResult[T]
Examples
>>> val x: Result[Result[String, Int], Int] = Err(Err("Some Error")) >>> x.flattenErr Err(Some Error) >>> val y: Result[Result[String, Int], Int] = Err(Ok(6)) >>> y.flattenErr Ok(6) >>> val z: Result[Result[String, Int], Int] = Ok(6) >>> z.flattenErr Ok(6) // Flattening only removes one level of nesting at a time: >>> val multi: Result[Result[Result[String, Int], Int], Int] = Err(Err(Err("Some Error"))) >>> multi.flattenErr Err(Err(Some Error)) >>> multi.flattenErr.flattenErr Err(Some Error)
- def fold[O](fErr: (E) => O, fOk: (T) => O): O
Applies
fOk
if this is anOk
orfErr
if this is anErr
Applies
fOk
if this is anOk
orfErr
if this is anErr
Examples
>>> val ok = Ok[Int, Int](1) >>> ok.fold(_ - 1, _ + 1) 2 >>> val err = Err[Int, Int](-1) >>> err.fold(_ - 1, _ + 1) -2
- def forall(p: (T) => Boolean): Boolean
Returns
true
ifErr
or returns the result of the application of the given predicate to theOk
value.Returns
true
ifErr
or returns the result of the application of the given predicate to theOk
value.Examples
>>> Ok(12).forall(_ > 10) true >>> Ok(7).forall(_ > 10) false >>> Err[Int, Int](12).forall(_ => false) true
- def forallErr(p: (E) => Boolean): Boolean
Returns
true
ifOk
or returns the result of the application of the given predicate to theErr
value.Returns
true
ifOk
or returns the result of the application of the given predicate to theErr
value.Examples
>>> Err(12).forallErr(_ > 10) true >>> Err(7).forallErr(_ > 10) false >>> Ok[Int, Int](12).forallErr(_ => false) true
- def foreach[U](op: (T) => U): Unit
An alias of inspect for compatibility with for-comprehensions and Scala naming
- def foreachErr[F](op: (E) => F): Unit
An alias of inspectErr for consistency with Scala naming (
Err
suffix required for disambiguation) - def hashCode(): Int
- Definition Classes
- Any
- def inspect[U](op: (T) => U): Unit
Executes the given side-effecting function if this is an
Ok
.Executes the given side-effecting function if this is an
Ok
.Examples
Err[String, Int]("Some Error").inspect(println(_)) // Doesn't print Ok(5).inspect(println(_)) // Prints 5
- def inspectErr[F](op: (E) => F): Unit
Executes the given side-effecting function if this is an
Err
.Executes the given side-effecting function if this is an
Err
.Examples
Ok[Int, String]("Some Value").inspectErr(println(_)) // Doesn't print Err(5).inspectErr(println(_)) // Prints 5
- def intoErr(implicit ev: <:<[T, Nothing]): E
Returns the contained
Err
value, but never throwsReturns the contained
Err
value, but never throwsUnlike unwrapErr, this method is known to never throw on the result types it is implemented for. Therefore, it can be used instead of unwrapErr as a maintainability safeguard that will fail to compile if the error type of the
Result
is later changed to an error that can actually occur.To leverage this method, the result must match
Result[_, Nothing]
. BecauseNothing
can never be instantiated, we can be assured that if the error type isNothing
then anErr
cannot be instantiated.Examples
>>> def onlyBadNews(msg: String): Result[String, Nothing] = Err("This msg is unacceptable: " + msg) >>> onlyBadNews("Some Error").intoErr This msg is unacceptable: Some Error >>> val possibleOkay: Result[String, Int] = Err("Some Error") possibleOkay.intoErr // This line would fail to compile because [[intoErr]] cannot prove it isn't an `Ok`.
- def intoOk(implicit ev: <:<[E, Nothing]): T
Returns the contained
Ok
value, but never throwsReturns the contained
Ok
value, but never throwsUnlike unwrap, this method is known to never throw on the result types it is implemented for. Therefore, it can be used instead of unwrap as a maintainability safeguard that will fail to compile if the error type of the
Result
is later changed to an error that can actually occur.To leverage this method, the result must match
Result[Nothing, _]
. BecauseNothing
can never be instantiated, we can be assured that if the error type isNothing
then anErr
cannot be instantiated.Examples
>>> def onlyGoodNews(msg: String): Result[Nothing, String] = Ok("This msg is fine: " + msg) >>> onlyGoodNews("Some Message").intoOk This msg is fine: Some Message >>> val possibleError: Result[Int, String] = Ok("Some Message") possibleError.intoOk // This line would fail to compile because [[intoOk]] cannot prove it isn't an `Err`.
- def intoOkOrErr[R](implicit vr: <:<[T, R], er: <:<[E, R]): R
Returns the contained
Ok
value if anOk
, and the containedErr
value if anErr
Returns the contained
Ok
value if anOk
, and the containedErr
value if anErr
In other words, this function returns the value (the
R
) of aResult[R, R]
, regardless of whether or not that result isOk
orErr
. This can be useful in rare cases when it doesn't matter whether the result was a success or failure.Examples
>>> val ok: Result[Int, Int] = Ok(3) >>> val err: Result[Int, Int] = Err(4) >>> ok.intoOkOrErr 3 >>> err.intoOkOrErr 4
- def isErr: Boolean
Returns
true
if the result isErr
.Returns
true
if the result isErr
.Examples
>>> val x: Result[String, Int] = Ok(-3) >>> x.isErr false >>> val y: Result[String, Int] = Err("Some error message") >>> y.isErr true
- def isErrAnd(f: (E) => Boolean): Boolean
Returns
true
if the result isErr
and the value inside of it matches a predicate.Returns
true
if the result isErr
and the value inside of it matches a predicate.Examples
>>> val x: Result[Int, String] = Err(2) >>> x.isErrAnd(_ > 1) true >>> val y: Result[Int, String] = Err(0) >>> y.isErrAnd(_ > 1) false >>> val z: Result[Int, String] = Ok("Some success string") >>> z.isErrAnd(_ > 1) false
- def isErrOr(f: (T) => Boolean): Boolean
Returns
true
if the result isErr
or the okay value matches a predicate.Returns
true
if the result isErr
or the okay value matches a predicate.Examples
>>> val x: Result[Int, String] = Err(2) >>> x.isErrOr(_ == "Foo") true >>> val y: Result[Int, String] = Ok("Foo") >>> y.isErrOr(_ == "Foo") true >>> val z: Result[Int, String] = Ok("Bar") >>> z.isErrOr(_ == "Foo") false
- final def isInstanceOf[T0]: Boolean
- Definition Classes
- Any
- def isOk: Boolean
Returns
true
if the result isOk
.Returns
true
if the result isOk
.Examples
>>> val x: Result[String, Int] = Ok(-3) >>> x.isOk true >>> val y: Result[String, Int] = Err("Some error message") >>> y.isOk false
- def isOkAnd(f: (T) => Boolean): Boolean
Returns
true
if the result isOk
and the value inside of it matches a predicate.Returns
true
if the result isOk
and the value inside of it matches a predicate.Examples
>>> val x: Result[String, Int] = Ok(2) >>> x.isOkAnd(_ > 1) true >>> val y: Result[String, Int] = Ok(0) >>> y.isOkAnd(_ > 1) false >>> val z: Result[String, Int] = Err("hey") >>> z.isOkAnd(_ > 1) false
- def isOkOr(f: (E) => Boolean): Boolean
Returns
true
if the result isOk
or the error value matches a predicate.Returns
true
if the result isOk
or the error value matches a predicate.Examples
>>> val x: Result[String, Int] = Ok(2) >>> x.isOkOr(_ == "Foo") true >>> val y: Result[String, Int] = Err("Foo") >>> y.isOkOr(_ == "Foo") true >>> val z: Result[String, Int] = Err("Bar") >>> z.isOkOr(_ == "Foo") false
- def joinErr[F, U >: T](implicit ev: <:<[E, Result[F, U]]): Result[F, U]
An alias of flattenErr for consistency with
Either
API, analogous tojoinLeft
- def joinOk[F >: E, U](implicit ev: <:<[T, Result[F, U]]): Result[F, U]
An alias of flatten for consistency with
Either
API, analogous tojoinRight
- def map[U](op: (T) => U): Result[E, U]
Maps a
Result[E, T]
toResult[E, U]
by applying a function to a containedOk
value, leaving anErr
value untouched.Maps a
Result[E, T]
toResult[E, U]
by applying a function to a containedOk
value, leaving anErr
value untouched.This function can be used to compose the results of two functions.
Examples
>>> def toInt(c: Char) = if (c.isDigit) Ok(c.asDigit) else Err("Not a digit") >>> def square(i: Int) = i * i >>> toInt('1').map(square(_)) Ok(1) >>> toInt('2').map(square(_)) Ok(4) >>> toInt('A').map(square(_)) Err(Not a digit)
- def mapErr[F](op: (E) => F): Result[F, T]
Maps a
Result[E, T]
toResult[F, T]
by applying a function to a containedErr
value, leaving anOk
value untouched.Maps a
Result[E, T]
toResult[F, T]
by applying a function to a containedErr
value, leaving anOk
value untouched.This function can be used to pass through a successful result while handling an error.
Examples
>>> def square(i: Int) = i * i >>> Err(1).mapErr(square(_)) Err(1) >>> Err(2).mapErr(square(_)) Err(4) >>> Ok[Int, String]("Some Value").mapErr(square(_)) Ok(Some Value)
- def mapErrOr[F](default: => F, f: (E) => F): F
Returns the provided default (if
Ok
), or applies a function to the contained value (ifErr
).Returns the provided default (if
Ok
), or applies a function to the contained value (ifErr
).Examples
>>> val x: Result[String, String] = Err("foo") >>> x.mapErrOr(42, _.size) 3 >>> val y: Result[String, String] = Ok("bar") >>> y.mapErrOr(42, _.size) 42
- def mapErrOrElse[F](default: (T) => F, f: (E) => F): F
Maps a
Result[E, T]
toF
by applying fallback function default to a containedOk
value, or functionf
to a containedErr
value.Maps a
Result[E, T]
toF
by applying fallback function default to a containedOk
value, or functionf
to a containedErr
value.This function can be used to unpack a successful result while handling an error.
Examples
>>> val k = 21 >>> val x: Result[String, String] = Err("foo") >>> x.mapErrOrElse(_ => k * 2, _.size) 3 >>> val y: Result[String, String] = Ok("bar") >>> y.mapErrOrElse(_ => k * 2, _.size) 42
- def mapOr[U](default: U, f: (T) => U): U
Returns the provided default (if
Err
), or applies a function to the contained value (ifOk
).Returns the provided default (if
Err
), or applies a function to the contained value (ifOk
).Examples
>>> val x: Result[String, String] = Ok("foo") >>> x.mapOr(42, _.size) 3 >>> val y: Result[String, String] = Err("bar") >>> y.mapOr(42, _.size) 42
- def mapOrElse[U](default: (E) => U, f: (T) => U): U
Maps a
Result[E, T]
toU
by applying fallback function default to a containedErr
value, or functionf
to a containedOk
value.Maps a
Result[E, T]
toU
by applying fallback function default to a containedErr
value, or functionf
to a containedOk
value.This function can be used to unpack a successful result while handling an error.
Examples
>>> val k = 21 >>> val x: Result[String, String] = Ok("foo") >>> x.mapOrElse(_ => k * 2, _.size) 3 >>> val y: Result[String, String] = Err("bar") >>> y.mapOrElse(_ => k * 2, _.size) 42
- final def ok: Option[T]
Converts from
Result[E, T]
toOption[T]
.Converts from
Result[E, T]
toOption[T]
.Converts
this
into anOption[T]
, discarding the error, if any.Examples
>>> val x: Result[String, Int] = Ok(2) >>> x.ok == Some(2) true >>> val y: Result[String, Int] = Err("Nothing here") >>> y.ok == None true
- def or[F >: E, U >: T](rhs: => Result[F, U]): Result[F, U]
Returns
rhs
if theResult
isErr
, otherwise returns the thisOk
value.Returns
rhs
if theResult
isErr
, otherwise returns the thisOk
value.Examples
>>> val x1: Result[String, Int] = Ok(2) >>> val y1: Result[String, Int] = Err("late error") >>> x1.or(y1) Ok(2) >>> val x2: Result[String, Int] = Err("early error") >>> val y2: Result[String, Int] = Ok(2) >>> x2.or(y2) Ok(2) >>> val x3: Result[String, Int] = Err("not a 2") >>> val y3: Result[String, Int] = Err("late error") >>> x3.or(y3) Err(late error) >>> val x4: Result[String, Int] = Ok(2) >>> val y4: Result[String, Int] = Ok(100) >>> x4.or(y4) Ok(2)
- def orElse[F, U >: T](op: (E) => Result[F, U]): Result[F, U]
Calls
op
if the result isErr
, otherwise returns thisOk
value.Calls
op
if the result isErr
, otherwise returns thisOk
value.This function can be used for control flow based on result values.
An alias of flatMapErr
Examples
>>> def sq(x: Int): Result[Int, Int] = { Ok(x * x) } >>> def err(x: Int): Result[Int, Int] = { Err(x) } >>> Ok(2).orElse(sq).orElse(sq) Ok(2) >>> Ok(2).orElse(err).orElse(sq) Ok(2) >>> Err(3).orElse(sq).orElse(err) Ok(9) >>> Err(3).orElse(err).orElse(err) Err(3)
- def to[V](implicit fromResult: FromResult[E, T, V]): V
Allows for conversion of a
Result[E, T]
into an arbitrary typeV
Allows for conversion of a
Result[E, T]
into an arbitrary typeV
This should be used in conjunction with
Result.apply[E, T, V]
to constructResult
s from a user definedV
. This can be helpful when leveraging custom ADTs.Examples
>>> sealed trait Case >>> case class Bad(str: String) extends Case >>> case class Good(value: Int) extends Case >>> val goodInt = 1 >>> val good = Good(goodInt) >>> val badStr = "Error" >>> val bad = Bad(badStr) // A contrived example: // NOTE: Using `intoOkOrErr` directly should be preferred for this case >>> implicit val caseFromADTResult: FromResult[Bad, Good, Case] = _.intoOkOrErr >>> val okADT: Result[Bad, Good] = Ok(good) >>> okADT.to[Case] == good true >>> val errADT: Result[Bad, Good] = Err(bad) >>> errADT.to[Case] == bad true // A more interesting example: >>> implicit val caseFromPrimitiveResult: FromResult[String, Int, Case] = { ... case Ok(v) => Good(v) ... case Err(e) => Bad(e) ... } >>> val okPrimitive: Result[String, Int] = Ok(goodInt) >>> okPrimitive.to[Case] == good true >>> val errPrimitive: Result[String, Int] = Err(badStr) >>> errPrimitive.to[Case] == bad true
- def toEither: Either[E, T]
Returns an
Either
using theOk
value if it exists forRight
otherwise using theErr
value forLeft
.Returns an
Either
using theOk
value if it exists forRight
otherwise using theErr
value forLeft
.Examples
>>> Ok(9).toEither == Right(9) true >>> val ex = new Exception("Test Exception") >>> Err(12).toEither == Left(12) true
- def toOption: Option[T]
An alias of ok for consistency with Scala naming
- def toOptionErr: Option[E]
An alias of err for consistency with Scala naming (
Err
suffix required for disambiguation) - def toSeq: Seq[T]
Returns a
Seq
containing theOk
value if it exists or an emptySeq
if this is aErr
.Returns a
Seq
containing theOk
value if it exists or an emptySeq
if this is aErr
.Examples
>>> Ok(12).toSeq == Seq(12) true >>> Err(12).toSeq == Seq() true
- def toSeqErr: Seq[E]
Returns a
Seq
containing theErr
value if it exists or an emptySeq
if this is aOk
.Returns a
Seq
containing theErr
value if it exists or an emptySeq
if this is aOk
.Examples
>>> Err(12).toSeqErr == Seq(12) true >>> Ok(12).toSeqErr == Seq() true
- def toString(): String
- Definition Classes
- Any
- def toTry(implicit ev: <:<[E, Throwable]): Try[T]
Returns a
Try
using theOk
value if it exists forSuccess
otherwise using theErr
value forFailure
.Returns a
Try
using theOk
value if it exists forSuccess
otherwise using theErr
value forFailure
.NOTE: This method is only available if the
Err
value is aThrowable
Examples
>>> import scala.util.{Failure, Success} >>> Ok(12).toTry == Success(12) true >>> val ex = new Exception("Test Exception") >>> Err(ex).toTry == Failure(ex) true Err(12).toTry // This line should fail to compile
- def transposeFuture[U](implicit ev: <:<[T, Future[U]], executor: ExecutionContext): Future[Result[E, U]]
Transposes a
Result
of an OkFuture
into anFuture
of aResult
.Transposes a
Result
of an OkFuture
into anFuture
of aResult
.Ok(Future(_))
andErr(_)
will be mapped toFuture(Ok(_))
andFuture(Err(_))
.Examples
>>> import scala.concurrent.Future >>> implicit val ec: scala.concurrent.ExecutionContext = scala.concurrent.ExecutionContext.global >>> val x: Result[String, Future[Int]] = Ok(Future(5)) >>> x.transposeFuture.isInstanceOf[Future[Result[String, Int]]] true >>> val y: Result[String, Future[Int]] = Err("Some Error") >>> y.transposeFuture.isInstanceOf[Future[Result[String, Int]]] true
- def transposeFutureErr[F](implicit ev: <:<[E, Future[F]], executor: ExecutionContext): Future[Result[F, T]]
Transposes a
Result
of anErr
Future
into anFuture
of aResult
.Transposes a
Result
of anErr
Future
into anFuture
of aResult
.Err(Future(_))
andOk(_)
will be mapped toFuture(Err(_))
andFuture(Ok(_))
.Examples
>>> import scala.concurrent.Future >>> implicit val ec: scala.concurrent.ExecutionContext = scala.concurrent.ExecutionContext.global >>> val x: Result[Future[String], Int] = Ok(5) >>> x.transposeFutureErr.isInstanceOf[Future[Result[String, Int]]] true >>> val y: Result[Future[String], Int] = Err(Future("Some Error")) >>> y.transposeFutureErr.isInstanceOf[Future[Result[String, Int]]] true
- def transposeOption[U](implicit ev: <:<[T, Option[U]]): Option[Result[E, U]]
Transposes a
Result
of an OkOption
into anOption
of aResult
.Transposes a
Result
of an OkOption
into anOption
of aResult
.Ok(None)
will be mapped toNone
.Ok(Some(_))
andErr(_)
will be mapped toSome(Ok(_))
andSome(Err(_))
.Examples
>>> val x1: Result[String, Option[Int]] = Ok(Some(5)) >>> val x2: Option[Result[String, Int]] = Some(Ok(5)) >>> x1.transposeOption == x2 true >>> val y1: Result[String, Option[Int]] = Ok(None) >>> val y2: Option[Result[String, Int]] = None >>> y1.transposeOption == y2 true >>> val z1: Result[String, Option[Int]] = Err("Some Error") >>> val z2: Option[Result[String, Int]] = Some(Err("Some Error")) >>> z1.transposeOption == z2 true
- def transposeOptionErr[F](implicit ev: <:<[E, Option[F]]): Option[Result[F, T]]
Transposes a
Result
of anErr
Option
into anOption
of aResult
.Transposes a
Result
of anErr
Option
into anOption
of aResult
.Err(None)
will be mapped toNone
.Err(Some(_))
andOk(_)
will be mapped toSome(Err(_))
andSome(Ok(_))
.Examples
>>> val x1: Result[Option[Int], String] = Err(Some(5)) >>> val x2: Option[Result[Int, String]] = Some(Err(5)) >>> x1.transposeOptionErr == x2 true >>> val y1: Result[Option[Int], String] = Err(None) >>> val y2: Option[Result[Int, String]] = None >>> y1.transposeOptionErr == y2 true >>> val z1: Result[Option[Int], String] = Ok("Some Okay") >>> val z2: Option[Result[Int, String]] = Some(Ok("Some Okay")) >>> z1.transposeOptionErr == z2 true
- def unwrap: T
Returns the contained
Ok
value.Returns the contained
Ok
value.Because this function may panic, its use is generally discouraged. Instead, prefer to use pattern matching and handle the
Err
case explicitly, or call unwrapOr or unwrapOrElse.Throws
Throws if the value is an
Err
, with a exception message provided by theErr
's value.Examples
>>> val x: Result[String, Int] = Ok(2) >>> x.unwrap 2 >>> val y: Result[String, Int] = Err("emergency failure") >>> intercept[RuntimeException](y.unwrap).getMessage called `Result::unwrap` on an `Err` value: emergency failure
- Annotations
- @throws(classOf[RuntimeException])
- def unwrapErr: E
Returns the contained
Err
value.Returns the contained
Err
value.Because this function may panic, its use is generally discouraged. Instead, prefer to use pattern matching and handle the
Ok
case explicitly.Throws
Throws if the value is an
Ok
, with a exception message provided by theOk
's value.Examples
>>> val x: Result[Int, String] = Err(2) >>> x.unwrapErr 2 >>> val y: Result[Int, String] = Ok("unexpected success") >>> intercept[RuntimeException](y.unwrapErr).getMessage called `Result::unwrapErr` on an `Ok` value: unexpected success
- Annotations
- @throws(classOf[RuntimeException])
- def unwrapOr[U >: T](default: => U): U
Returns the contained
Ok
value or a provided default.Returns the contained
Ok
value or a provided default.Examples
>>> val default = 2 >>> val x: Result[String, Int] = Ok(9) >>> x.unwrapOr(default) 9 >>> val y: Result[String, Int] = Err("error") >>> y.unwrapOr(default) 2
- def unwrapOrElse[U >: T](op: (E) => U): U
Returns the contained
Ok
value or computes it from a provided function applied to theErr
value.Returns the contained
Ok
value or computes it from a provided function applied to theErr
value.Examples
>>> Ok[String, Int](2).unwrapOrElse(_.size) 2 >>> Err("foo").unwrapOrElse(_.size) 3
- def withErr[F >: E]: Result[F, T]
Upcasts this
Result[E, T]
toResult[F, T]
Upcasts this
Result[E, T]
toResult[F, T]
Normally used when constructing an
Ok
Examples
scala> Ok(1) res0: Ok[Nothing, Int] = Ok(1) scala> Ok(2).withErr[String] res1: Result[String, Int] = Ok(2)
- def withOk[U >: T]: Result[E, U]
Upcasts this
Result[E, T]
toResult[E, U]
Upcasts this
Result[E, T]
toResult[E, U]
Normally used when constructing an
Err
Examples
scala> Err(1) res0: Err[Int, Nothing] = Err(1) scala> Err(2).withOk[String] res1: Result[Int, String] = Err(2)