Object

com.thoughtworks.dsl.domains

cats

Related Doc: package domains

Permalink

object cats

Contains interpreters to enable !-notation for keywords.Monadic and other keywords in code blocks whose type support cats.FlatMap and cats.MonadError.

Author:

杨博 (Yang Bo)

Source
cats.scala
Examples:
  1. A @reset code block can contain try / catch / finally if the monadic data type supports cats.MonadError. For example, cats.effect.IO is a monadic data type that supports cats.MonadError, therefore try / catch / finally expressions can be used inside a @reset code block whose return type is cats.effect.IO.

    import com.thoughtworks.dsl.keywords.Monadic._
    import com.thoughtworks.dsl.domains.cats._
    import _root_.cats.effect.IO
    import com.thoughtworks.dsl.Dsl.reset
    val io0 = IO(0)
    def dslTryCatch: IO[String] = IO {
      try {
        s"Division result: ${!io0 / !io0}"
      } catch {
        case e: ArithmeticException =>
          s"Cannot divide ${!io0} by itself"
      }
    }: @reset
    dslTryCatch.unsafeRunSync() should be("Cannot divide 0 by itself")

    The above dslTryCatch method is equivalent to the following code in cats.syntax:

    def catsSyntaxTryCatch: IO[String] = {
      import _root_.cats.syntax.applicativeError._
      io0.flatMap { tmp0 =>
        io0.flatMap { tmp1 =>
           IO(s"Division result: ${tmp0 / tmp1}")
        }
      }.handleErrorWith {
        case e: ArithmeticException =>
          io0.flatMap { tmp2 =>
             IO(s"Cannot divide ${tmp2} by itself")
          }
        case e =>
          e.raiseError[IO, String]
      }
    }
    catsSyntaxTryCatch.unsafeRunSync() should be("Cannot divide 0 by itself")
  2. ,
  3. Trampoline is a monadic data type that performs tail call optimization. It can be built from a @reset code block within some !-notation, similar to the each method in ThoughtWorks Each.

    import _root_.cats.free.Trampoline
    import _root_.cats.instances.function._
    import com.thoughtworks.dsl.keywords.Monadic._
    import com.thoughtworks.dsl.domains.cats._
    import com.thoughtworks.dsl.Dsl.reset
    val trampoline3 = Trampoline.done(3)
    def dslSquare = Trampoline.delay {
      s"This string is produced by a trampoline: ${!trampoline3 * !trampoline3}"
    }: @reset
    dslSquare.run should be("This string is produced by a trampoline: 9")

    !trampoline3 is a shortcut of !Monadic(trampoline3), which will be converted to flatMap calls by our DSL interpreter. Thus, the method dslSquare is equivalent to the following code in cats.syntax:

    def catsSyntaxSquare = trampoline3.flatMap { tmp1 =>
      trampoline3.flatMap { tmp2 =>
        Trampoline.delay {
          s"This string is produced by a trampoline: ${tmp1 * tmp2}"
        }
      }
    }
    catsSyntaxSquare.run should be("This string is produced by a trampoline: 9")
Linear Supertypes
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. cats
  2. AnyRef
  3. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Type Members

  1. type MonadThrowable[F[_]] = MonadError[F, Throwable]

    Permalink
    Attributes
    protected

Value Members

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

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

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

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

    Permalink
    Definition Classes
    Any
  5. implicit def catsCatchDsl[F[_], A, B](implicit monadError: MonadThrowable[F]): CatchDsl[F[A], F[B], A]

    Permalink
  6. implicit def catsMonadicDsl[F[_], A, B](implicit flatMap: FlatMap[F]): Dsl[Monadic[F, A], F[B], A]

    Permalink
  7. implicit def catsReturnDsl[F[_], A, B](implicit applicative: Applicative[F], restReturnDsl: Dsl[Return[A], B, Nothing]): Dsl[Return[A], F[B], Nothing]

    Permalink
  8. def clone(): AnyRef

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  9. final def eq(arg0: AnyRef): Boolean

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

    Permalink
    Definition Classes
    AnyRef → Any
  11. def finalize(): Unit

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  12. final def getClass(): Class[_]

    Permalink
    Definition Classes
    AnyRef → Any
  13. def hashCode(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  14. final def isInstanceOf[T0]: Boolean

    Permalink
    Definition Classes
    Any
  15. final def ne(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  16. final def notify(): Unit

    Permalink
    Definition Classes
    AnyRef
  17. final def notifyAll(): Unit

    Permalink
    Definition Classes
    AnyRef
  18. final def synchronized[T0](arg0: ⇒ T0): T0

    Permalink
    Definition Classes
    AnyRef
  19. def toString(): String

    Permalink
    Definition Classes
    AnyRef → Any
  20. final def wait(): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  21. final def wait(arg0: Long, arg1: Int): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  22. final def wait(arg0: Long): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )

Inherited from AnyRef

Inherited from Any

Ungrouped