com.thoughtworks.tryt.invariant
A monad transformer for exception handling.
As a monad transformer, TryT should be used with another monadic data type, like scalaz.Name.
TryT
import scalaz.Name import com.thoughtworks.tryt.invariant.TryT, TryT._ type TryName[A] = TryT[Name, A]
Given a validate function,
validate
def validate(s: String): Int = s.toInt
when creating a TryT-transformed scalaz.Name from the validate,
import scalaz.syntax.all._ val invalidTry: TryName[Int] = validate("invalid input").point[TryName]
then the exceptions thrown in validate call should be converted to a scala.util.Failure;
import com.thoughtworks.tryt.invariant.TryT._ val TryT(Name(failure)) = invalidTry import scala.util._ failure should be(an[Failure[_]])
and when there is no exception thrown in validate call,
val validTry: TryName[Int] = validate("42").point[TryName]
then the result of validate call should be converted to a scala.util.Success;
val TryT(Name(success)) = validTry success should be(Success(42))
and when the TryT-transformed scalaz.Name is built from a for-comprehension,
for
val invalidForComprehension: TryName[Int] = for { i <- validate("42").point[TryName] j <- validate("invalid input").point[TryName] } yield i + j
then the exceptions thrown in the for-comprehension should be converted to a scala.util.Failure;
val TryT(Name(failure2)) = invalidTry failure2 should be(an[Failure[_]])
This TryT type is an opacity alias to F[Try[A]]. All type classes and helper functions for this TryT type are defined in the companion object TryT
F[Try[A]]
This TryT transfomer is similar to scalaz.EitherT, except TryT handles exceptions thrown in callback functions passed to map, bind or point.
A monad transformer for exception handling.
As a monad transformer,
TryT
should be used with another monadic data type, like scalaz.Name.Given a
validate
function,when creating a
TryT
-transformed scalaz.Name from thevalidate
,then the exceptions thrown in
validate
call should be converted to a scala.util.Failure;and when there is no exception thrown in
validate
call,then the result of
validate
call should be converted to a scala.util.Success;and when the
TryT
-transformed scalaz.Name is built from afor
-comprehension,then the exceptions thrown in the
for
-comprehension should be converted to a scala.util.Failure;val TryT(Name(failure2)) = invalidTry failure2 should be(an[Failure[_]])
This
TryT
type is an opacity alias toF[Try[A]]
. All type classes and helper functions for thisTryT
type are defined in the companion object TryTThis
TryT
transfomer is similar to scalaz.EitherT, exceptTryT
handles exceptions thrown in callback functions passed to map, bind or point.