Type/Object

com.thoughtworks.zerocost.tryt

TryT

Related Docs: object TryT | package tryt

Permalink

type TryT[F[+_], +A] = tryt.OpacityTypes.TryT[F, A]

A monad transformer for exception handling.

Source
tryt.scala
Example:
  1. As a monad transformer, TryT should be used with another monadic data type, like scala.Function0.

    import cats.instances.function._
    import scala.util._
    import com.thoughtworks.zerocost.tryt._
    type TryName[+A] = TryT[Function0, A]

    Given a validate function,

    def validate(s: String): Int = s.toInt

    when creating a TryT-transformed scala.Function0 from the validate,

    import cats.syntax.all._
    val invalidTry: TryName[Int] = TryT(() => Try(validate("invalid input")))

    then the exceptions thrown in validate call should be converted to a scala.util.Failure;

    val TryT(failure) = invalidTry
    failure() should be(an[Failure[_]])

    and when there is no exception thrown in validate call,

    val validTry: TryName[Int] = validate("42").pure[TryName]

    then the result of validate call should be converted to a scala.util.Success;

    val TryT(success) = validTry
    success() should be(Success(42))

    and when the TryT-transformed scala.Function0 is built from a for-comprehension,

    val invalidForComprehension: TryName[Int] = validate("42").pure[TryName].flatMap { i =>
      validate("invalid input").pure[TryName].map { j =>
        i + j
      }
    }

    then the exceptions thrown in the for-comprehension should be converted to a scala.util.Failure;

    val TryT(failure2) = invalidTry
    failure2() should be(an[Failure[_]])
Note

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

See also

This TryT transfomer is similar to cats.data.EitherT, except TryT handles exceptions thrown in callback functions passed to map, flatMap or pure.

Visibility
  1. Public
  2. All

Abstract Value Members

  1. abstract def getClass(): Class[_]

    Permalink
    Definition Classes
    Any

Concrete Value Members

  1. final def !=(arg0: Any): Boolean

    Permalink
    Definition Classes
    Any
  2. final def ##(): Int

    Permalink
    Definition Classes
    Any
  3. final def ==(arg0: Any): Boolean

    Permalink
    Definition Classes
    Any
  4. final def asInstanceOf[T0]: T0

    Permalink
    Definition Classes
    Any
  5. def equals(arg0: Any): Boolean

    Permalink
    Definition Classes
    Any
  6. def hashCode(): Int

    Permalink
    Definition Classes
    Any
  7. final def isInstanceOf[T0]: Boolean

    Permalink
    Definition Classes
    Any
  8. def toString(): String

    Permalink
    Definition Classes
    Any

Ungrouped