Created
January 30, 2018 21:35
-
-
Save fedefernandez/1a2dc9830f4d9a8b91fe4d188337b1bd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cats.{Applicative, Monad} | |
import freestyle.tagless._ | |
import freestyle.tagless.logging.LoggingM | |
import scala.util.Try | |
@tagless trait MyService1[F[_]] { | |
def method1(string: String): F[Int] | |
} | |
@tagless trait MyService2[F[_]] { | |
def method2(int: Int): F[Boolean] | |
} | |
@module trait MyModule[F[_]] { | |
import cats.syntax.flatMap._ | |
import cats.syntax.functor._ | |
implicit val M: Monad[F] | |
val myService1: MyService1[F] | |
val myService2: MyService2[F] | |
val loggingM: LoggingM[F] | |
def moduleMethod: F[Boolean] = { | |
for { | |
_ <- loggingM.debug("Step 1") | |
v1 <- myService1.method1("Hello World") | |
_ <- loggingM.debug("Step 2") | |
v2 <- myService2.method2(v1) | |
_ <- loggingM.debug("Step 3") | |
} yield v2 | |
} | |
} | |
object Implicits { | |
implicit def myService1Handler[F[_]: Applicative]: MyService1.Handler[F] = | |
new MyService1.Handler[F] { | |
def method1(string: String): F[Int] = Applicative[F].pure(string.length) | |
} | |
implicit def myService2Handler[F[_]: Applicative]: MyService2.Handler[F] = | |
new MyService2.Handler[F] { | |
def method2(int: Int): F[Boolean] = Applicative[F].pure(int % 2 == 0) | |
} | |
} | |
object Program extends App { | |
import cats.instances.try_._ | |
import Implicits._ | |
import freestyle.tagless.loggingJVM.journal.implicits._ | |
// Or you can implement your own LoggingM.Handler like this: | |
// https://github.com/frees-io/freestyle/blob/master/modules/logging/jvm/src/main/scala/tagless/journal.scala#L26 | |
val myModule: MyModule[Try] = MyModule[Try] | |
println(myModule.moduleMethod) | |
// Success(false) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment