- Notifications
You must be signed in to change notification settings - Fork68
ZIO instances for cats-effect type classes
License
zio/interop-cats
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
libraryDependencies+="dev.zio"%%"zio-interop-cats"%"<latest-version>"
ZIO integrates with Typelevel libraries by providing an instance ofConcurrent
,Temporal
andAsync
forTask
as required, for instance, byfs2
,doobie
andhttp4s
.
For convenience, the ZIO library defines an alias as follows:
typeTask[A]=ZIO[Any,Throwable,A]
Therefore, we provide Cats Effect instances based on this specific datatype.
In order to get aConcurrent[Task]
orConcurrent[RIO[R, *]]
(note*
is kind-projector notation) we need to importzio.interop.catz._
:
importcats.effect._importzio._importzio.interop.catz._defceConcurrentForTaskExample= {valF: cats.effect.Concurrent[Task]= implicitlyF.racePair(F.unit,F.unit)}
importcats.effect._importzio._importzio.interop.catz._defceTemporal= {valF: cats.effect.Temporal[Task]= implicitlyF.sleep(1.second)*>F.unit}
importcats.effect._importzio._importzio.interop.catz._defceAsync= {valF: cats.effect.Async[Task]= implicitlyF.racePair(F.unit,F.sleep(1.second)*>F.unit)}
There are many other typeclasses and useful conversions that this library provides implementations for:
- See
zio/interop/cats.scala
file to see all available typeclass implementations for the Cats Effect 3 typeclasses - See
zio/stream/interop/cats.scala
for ZStream typeclass implementations - See
zio/stream/interop/FS2StreamSyntax.scala
for FS2 <-> ZStream conversions
If you only need instances forcats-core
typeclasses, notcats-effect
importzio.interop.catz.core._
:
importzio.interop.catz.core._
Note that this library only has anOptional
dependency on cats-effect – if you or your libraries don't depend on it, this library will not add it to the classpath.
The following example shows how to use ZIO with Doobie (a library for JDBC access) and FS2 (a streaming library), which both rely on Cats Effect instances (cats.effect.Async
andcats.effect.Temporal
):
importzio.{durationIntas_,*}importzio.interop.catz.*importdoobie.*importdoobie.implicits.*importscala.concurrent.duration.*objectExampleextendsZIOAppDefault:valrun= {valxa:Transactor[Task]=Transactor.fromDriverManager[Task]("org.h2.Driver","jdbc:h2:mem:test;DB_CLOSE_DELAY=-1","user","",None)sql"SELECT 42" .query[Int] .stream .transact(xa) .delayBy(1.second) .evalTap(i=>Console.printLine(i)) .compile .drain .exitCode }
Learn more on theZIO Interop Cats homepage!
For the general guidelines, see ZIOcontributor's guide.
See theCode of Conduct
About
ZIO instances for cats-effect type classes