Comfy Cats Effect room. Ask questions. Rant about config file formats. Be nice!
IO
and Task
Task
Hi.
Is this behavior of bracket
correct?
import cats.data.{Chain, WriterT}
import cats.effect.{ExitCode, IO, IOApp, Sync}
import cats.effect.implicits._
import cats.implicits._
import cats.mtl.FunctorTell
import cats.mtl.implicits._
object Test extends IOApp {
def bracketWithTell[F[_], A](a: A)(implicit F: Sync[F], W: FunctorTell[F, Chain[String]]): F[ExitCode] =
a.pure[F].bracket { x =>
F.delay(println(s"used $x")) *> W.tell(Chain.one(s"used $x")) as ExitCode.Success
} { x =>
F.delay(println(s"released $x")) *> W.tell(Chain.one(s"released $x"))
}
override def run(args: List[String]): IO[ExitCode] =
bracketWithTell[WriterT[IO, Chain[String], ?], Int](1).run.flatMap {
case (log, code) => IO(println(s"writer log: $log")) as code
}
}
Output:
used 1
released 1
writer log: Chain(used 1)
We lose the writer effect in the release
part. The same holds for all transformers.
If it's correct, doesn't it need clarification in docs?
WriterT
or StateT
.
parMapN
example missing ContextShift[IO]
. I thought it could be helpful to add one way to provide contextShift helpful noobs like me. So created a pull request https://github.com/typelevel/cats-effect/pull/372/files
Learning Question: I'm trying to run the cats.effect.Resource
example from https://typelevel.org/cats-effect/datatypes/resource.html#example. I get value map is not a member of cats.effect.Resource[cats.effect.IO,String]
. Wondering why is it looking for map
not flatMap
,
// example taken from https://typelevel.org/cats-effect/datatypes/resource.html#example
test("cats Resource") {
import cats.effect.{IO, Resource}
import cats.implicits._
def mkResource(data: String): Resource[IO, String] = {
val acquire = IO(println(s"Acquiring $data")) *> IO.pure(data)
def release(res: String) = IO(println(s"Releasing $res"))
Resource.make(acquire)(release)
}
val out: Resource[IO, String] = mkResource("outer")
val in: Resource[IO, String] = mkResource("inner")
// val rr = out.flatMap { a =>
// in.flatMap { b =>
// Resource.pure((a, b))
// }
// }
// out.map { o =>
// in.map { i =>
// (o, i)
// }
// }
val r = for {
outer <- out
inner <- in
} yield (outer, inner)
r.use { case (x, y) => IO(println(s"Using $x and $y")) }.unsafeRunSync
}
scalacOptions += "-Ypartial-unification"
Resource[IO, String]
(G[_, _]
) needs to fit into F[_]
so that you can fetch Monad[F]
to get flatMap
F[_]
be Resource[IO, ?]
Resource
so it can unify with F[_]