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
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 cats.free.Trampoline import 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 toflatMap
calls by our DSL interpreter. Thus, the methoddslSquare
is equivalent to the following code in cats.syntax:def catsSyntaxSquare = trampoline3.flatMap { tmp1 => import cats.syntax.flatMap._ 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")
A@reset
code block can containtry
/catch
/finally
if the monadic data type supports cats.MonadError. cats.effect.IO is a monadic data type that supports cats.MonadError, thereforetry
/catch
/finally
expressions can be used inside a@reset
code block whose return type is cats.effect.IO.import cats.effect.IO 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 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")
- To do
Monadic should be a scala.AnyVal after https://github.com/scala/bug/issues/10595 is resolved.
- Alphabetic
- By Inheritance
- cats
- AnyRef
- Any
- Hide All
- Show All
- Public
- All
Type Members
-
type
MonadThrowable[F[_]] = MonadError[F, Throwable]
- Attributes
- protected
Value Members
-
final
def
!=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
##(): Int
- Definition Classes
- AnyRef → Any
-
final
def
==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
asInstanceOf[T0]: T0
- Definition Classes
- Any
- implicit def catsCatchDsl[F[_], A](implicit monadError: MonadThrowable[F]): Dsl[Catch[F[A]], F[A], Unit]
- implicit def catsFlatMapDsl[F[_], A, B](implicit flatMap: FlatMap[F]): Dsl[Monadic[F, A], F[B], A]
- implicit def catsScopeDsl[F[_], A, B](implicit monadError: MonadError[F, Throwable]): Dsl[Scope[F[B], A], F[B], A]
-
def
clone(): AnyRef
- Attributes
- protected[java.lang]
- Definition Classes
- AnyRef
- Annotations
- @native() @throws( ... )
-
final
def
eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
def
equals(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
def
finalize(): Unit
- Attributes
- protected[java.lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( classOf[java.lang.Throwable] )
-
final
def
getClass(): Class[_]
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
-
def
hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
-
final
def
isInstanceOf[T0]: Boolean
- Definition Classes
- Any
-
final
def
ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
final
def
notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
-
final
def
notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
-
final
def
synchronized[T0](arg0: ⇒ T0): T0
- Definition Classes
- AnyRef
-
def
toString(): String
- Definition Classes
- AnyRef → Any
-
final
def
wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @native() @throws( ... )