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
rhsif the result isOk, otherwise returns thisErrvalue.Returns
rhsif the result isOk, otherwise returns thisErrvalue.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
opif theResultisOk, otherwise returns thisErrvalue.Calls
opif theResultisOk, otherwise returns thisErrvalue.This function can be used for control flow based on
Resultvalues. 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
trueif the result is anOkvalue containing the given value.Returns
trueif the result is anOkvalue 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
trueif the result is anErrvalue containing the given value.Returns
trueif the result is anErrvalue 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
thisinto 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
falseifError returns the result of the application of the given predicate to theOkvalue.Returns
falseifError returns the result of the application of the given predicate to theOkvalue.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
falseifOkor returns the result of the application of the given predicate to theErrvalue.Returns
falseifOkor returns the result of the application of the given predicate to theErrvalue.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
Okvalue.Returns the contained
Okvalue.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
Errvalue.Returns the contained
Errvalue.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
Errif this is anErrand the predicatepholds for the err value, orOk(default)if the predicatepdoes not hold, else returns the existingOk.Returns the existing
Errif this is anErrand the predicatepholds for the err value, orOk(default)if the predicatepdoes 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
Okif this is anOkand the predicatepholds for the ok value, orErr(default)if the predicatepdoes not hold, else returns the existingErr.Returns the existing
Okif this is anOkand the predicatepholds for the ok value, orErr(default)if the predicatepdoes 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 (
Errsuffix 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
fOkif this is anOkorfErrif this is anErrApplies
fOkif this is anOkorfErrif this is anErrExamples
>>> 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
trueifError returns the result of the application of the given predicate to theOkvalue.Returns
trueifError returns the result of the application of the given predicate to theOkvalue.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
trueifOkor returns the result of the application of the given predicate to theErrvalue.Returns
trueifOkor returns the result of the application of the given predicate to theErrvalue.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 (
Errsuffix 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
Errvalue, but never throwsReturns the contained
Errvalue, 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
Resultis later changed to an error that can actually occur.To leverage this method, the result must match
Result[_, Nothing]. BecauseNothingcan never be instantiated, we can be assured that if the error type isNothingthen anErrcannot 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
Okvalue, but never throwsReturns the contained
Okvalue, 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
Resultis later changed to an error that can actually occur.To leverage this method, the result must match
Result[Nothing, _]. BecauseNothingcan never be instantiated, we can be assured that if the error type isNothingthen anErrcannot 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
Okvalue if anOk, and the containedErrvalue if anErrReturns the contained
Okvalue if anOk, and the containedErrvalue if anErrIn other words, this function returns the value (the
R) of aResult[R, R], regardless of whether or not that result isOkorErr. 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
trueif the result isErr.Returns
trueif 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
trueif the result isErrand the value inside of it matches a predicate.Returns
trueif the result isErrand 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
trueif the result isError the okay value matches a predicate.Returns
trueif the result isError 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
trueif the result isOk.Returns
trueif 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
trueif the result isOkand the value inside of it matches a predicate.Returns
trueif the result isOkand 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
trueif the result isOkor the error value matches a predicate.Returns
trueif the result isOkor 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
EitherAPI, analogous tojoinLeft - def joinOk[F >: E, U](implicit ev: <:<[T, Result[F, U]]): Result[F, U]
An alias of flatten for consistency with
EitherAPI, 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 containedOkvalue, leaving anErrvalue untouched.Maps a
Result[E, T]toResult[E, U]by applying a function to a containedOkvalue, leaving anErrvalue 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 containedErrvalue, leaving anOkvalue untouched.Maps a
Result[E, T]toResult[F, T]by applying a function to a containedErrvalue, leaving anOkvalue 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]toFby applying fallback function default to a containedOkvalue, or functionfto a containedErrvalue.Maps a
Result[E, T]toFby applying fallback function default to a containedOkvalue, or functionfto a containedErrvalue.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]toUby applying fallback function default to a containedErrvalue, or functionfto a containedOkvalue.Maps a
Result[E, T]toUby applying fallback function default to a containedErrvalue, or functionfto a containedOkvalue.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
thisinto 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
rhsif theResultisErr, otherwise returns the thisOkvalue.Returns
rhsif theResultisErr, otherwise returns the thisOkvalue.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
opif the result isErr, otherwise returns thisOkvalue.Calls
opif the result isErr, otherwise returns thisOkvalue.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 typeVAllows for conversion of a
Result[E, T]into an arbitrary typeVThis should be used in conjunction with
Result.apply[E, T, V]to constructResults 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
Eitherusing theOkvalue if it exists forRightotherwise using theErrvalue forLeft.Returns an
Eitherusing theOkvalue if it exists forRightotherwise using theErrvalue 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 (
Errsuffix required for disambiguation) - def toSeq: Seq[T]
Returns a
Seqcontaining theOkvalue if it exists or an emptySeqif this is aErr.Returns a
Seqcontaining theOkvalue if it exists or an emptySeqif this is aErr.Examples
>>> Ok(12).toSeq == Seq(12) true >>> Err(12).toSeq == Seq() true
- def toSeqErr: Seq[E]
Returns a
Seqcontaining theErrvalue if it exists or an emptySeqif this is aOk.Returns a
Seqcontaining theErrvalue if it exists or an emptySeqif 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
Tryusing theOkvalue if it exists forSuccessotherwise using theErrvalue forFailure.Returns a
Tryusing theOkvalue if it exists forSuccessotherwise using theErrvalue forFailure.NOTE: This method is only available if the
Errvalue is aThrowableExamples
>>> 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
Resultof an OkFutureinto anFutureof aResult.Transposes a
Resultof an OkFutureinto anFutureof 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
Resultof anErrFutureinto anFutureof aResult.Transposes a
Resultof anErrFutureinto anFutureof 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
Resultof an OkOptioninto anOptionof aResult.Transposes a
Resultof an OkOptioninto anOptionof 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
Resultof anErrOptioninto anOptionof aResult.Transposes a
Resultof anErrOptioninto anOptionof 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
Okvalue.Returns the contained
Okvalue.Because this function may panic, its use is generally discouraged. Instead, prefer to use pattern matching and handle the
Errcase 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
Errvalue.Returns the contained
Errvalue.Because this function may panic, its use is generally discouraged. Instead, prefer to use pattern matching and handle the
Okcase 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
Okvalue or a provided default.Returns the contained
Okvalue 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
Okvalue or computes it from a provided function applied to theErrvalue.Returns the contained
Okvalue or computes it from a provided function applied to theErrvalue.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
OkExamples
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
ErrExamples
scala> Err(1) res0: Err[Int, Nothing] = Err(1) scala> Err(2).withOk[String] res1: Result[Int, String] = Err(2)