trait
FutureSugar extends AnyRef
Type Members
-
implicit
class
Futureizer[T] extends AnyRef
Value Members
-
final
def
!=(arg0: AnyRef): Boolean
-
final
def
!=(arg0: Any): Boolean
-
final
def
##(): Int
-
final
def
==(arg0: AnyRef): Boolean
-
final
def
==(arg0: Any): Boolean
-
final
def
asInstanceOf[T0]: T0
-
def
clone(): AnyRef
-
final
def
eq(arg0: AnyRef): Boolean
-
def
equals(arg0: Any): Boolean
-
def
finalize(): Unit
-
final
def
getClass(): Class[_]
-
def
hashCode(): Int
-
final
def
isInstanceOf[T0]: Boolean
-
final
def
ne(arg0: AnyRef): Boolean
-
final
def
notify(): Unit
-
final
def
notifyAll(): Unit
-
final
def
synchronized[T0](arg0: ⇒ T0): T0
-
def
toString(): String
-
final
def
wait(): Unit
-
final
def
wait(arg0: Long, arg1: Int): Unit
-
final
def
wait(arg0: Long): Unit
Inherited from AnyRef
Inherited from Any
Trait providing an implicit class that adds a
validatingmethod toFuture, which takes one or more validation functions and returns either the sameFutureif either theFuturehad already failed or its value passes all the functions, orValidationFailedExceptioncontaining an error message describing the first validation that failed.Here's an example validation method, which passes if the given
Intis evenly divisible by 10 (i.e., the result will bePass). If the value does not pass this test, the result is aFailcontaining a helpful error message string.scala> import org.scalactic._ import org.scalactic._ scala> import FutureSugar._ import org.scalactic.FutureSugar._ scala> import scala.concurrent.Future import scala.concurrent.Future scala> import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.ExecutionContext.Implicits.global scala> def isRound(i: Int): Validation[ErrorMessage] = | if (i % 10 == 0) Pass else Fail(i + " was not a round number") isRound: (i: Int)org.scalactic.Validation[org.scalactic.ErrorMessage]Validation will be attempted on a successful
Try. If the validation succeeds, the resultingFuturewill be the same successfulFuturewith the same value. (A "validation" only transforms theFutureif the validation fails, otherwise it is the sameFuture. The only difference is its value has now been proven valid.) In the following example, a successfulFuture[Int]with the value 100 passes the validation (which checks whether 100 is evenly divisible by 10), therefore the result of thevalidatingcall is the same successfulFuturewith the same value.If validation fails, the successful
Futurewill be transformed into a failed one, with aValidationFailedExceptionthat contains the error message returned by the validation function. In the following example, 42 fails the validation because it is not evenly divisible by 10:If
validatingis called on a failedFuture, it just returns the same failedFuture:scala> val futEx = Future[Int] { throw new Exception("oops!") } futEx: scala.concurrent.Future[Int] = scala.concurrent.impl.Promise$DefaultPromise@3ba0299c scala> futEx.value res4: Option[scala.util.Try[Int]] = Some(Failure(java.lang.Exception: oops!)) scala> val roundEx = futEx.validating(isRound) roundEx: scala.concurrent.Future[Int] = scala.concurrent.impl.Promise$DefaultPromise@22bf1acf scala> roundEx.value res5: Option[scala.util.Try[Int]] = Some(Failure(java.lang.Exception: oops!))The
validatingmethod accepts one or more validation functions. If you pass more than one, they will be tried in order up until the first failure, whose error message will appear in theValidationFailedException. In other words,validatingwill short circuit at the first error and return that. It will not accumulate errors. For example, the following validation will short circuit after theisDivBy3function fails:scala> def isDivBy3(i: Int): Validation[ErrorMessage] = | if (i % 3 == 0) Pass else Fail(i + " was not divisible by 3") isDivBy3: (i: Int)org.scalactic.Validation[org.scalactic.ErrorMessage] scala> def isAnswerToLifeTheUniverseAndEverything(i: Int): Validation[ErrorMessage] = | if (i == 42) Pass else Fail(i + " did not equal 42") isAnswerToLifeTheUniverseAndEverything: (i: Int)org.scalactic.Validation[org.scalactic.ErrorMessage] scala> val futShort = fut100.validating(isRound, isDivBy3, isAnswerToLifeTheUniverseAndEverything) futShort: scala.concurrent.Future[Int] = scala.concurrent.impl.Promise$DefaultPromise@30bb943e scala> futShort.value res11: Option[scala.util.Try[Int]] = Some(Failure(org.scalactic.exceptions.ValidationFailedException: 100 was not divisible by 3))