https://github.com/zio/interop-cats
ZIO instances for cats-effect type classes
https://github.com/zio/interop-cats
Last synced: 6 months ago
JSON representation
ZIO instances for cats-effect type classes
- Host: GitHub
- URL: https://github.com/zio/interop-cats
- Owner: zio
- License: apache-2.0
- Created: 2019-06-09T15:49:52.000Z (over 6 years ago)
- Default Branch: main
- Last Pushed: 2025-03-25T20:21:52.000Z (7 months ago)
- Last Synced: 2025-04-01T08:38:33.936Z (7 months ago)
- Language: Scala
- Homepage:
- Size: 837 KB
- Stars: 158
- Watchers: 14
- Forks: 68
- Open Issues: 17
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
[//]: # (This file was autogenerated using `zio-sbt-website` plugin via `sbt generateReadme` command.)
[//]: # (So please do not edit it manually. Instead, change "docs/index.md" file or sbt setting keys)
[//]: # (e.g. "readmeDocumentation" and "readmeSupport".)# ZIO Interop Cats
## Installation
```sbt
libraryDependencies += "dev.zio" %% "zio-interop-cats" % ""
```## `ZIO` Cats Effect 3 instances
**ZIO** integrates with Typelevel libraries by providing an instance of `Concurrent`, `Temporal` and `Async` for `Task`
as required, for instance, by `fs2`, `doobie` and `http4s`.For convenience, the ZIO library defines an alias as follows:
```scala
type Task[A] = ZIO[Any, Throwable, A]
```Therefore, we provide Cats Effect instances based on this specific datatype.
## `Concurrent`
In order to get a `Concurrent[Task]` or `Concurrent[RIO[R, *]]` (note `*` is kind-projector notation) we need to import `zio.interop.catz._`:
```scala
import cats.effect._
import zio._
import zio.interop.catz._def ceConcurrentForTaskExample = {
val F: cats.effect.Concurrent[Task] = implicitly
F.racePair(F.unit, F.unit)
}
```## `Temporal`
```scala
import cats.effect._
import zio._
import zio.interop.catz._def ceTemporal = {
val F: cats.effect.Temporal[Task] = implicitly
F.sleep(1.second) *> F.unit
}
```## `Async`
```scala
import cats.effect._
import zio._
import zio.interop.catz._def ceAsync = {
val F: cats.effect.Async[Task] = implicitly
F.racePair(F.unit, F.sleep(1.second) *> F.unit)
}
```## Other typeclasses
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### cats-core
If you only need instances for `cats-core` typeclasses, not `cats-effect` import `zio.interop.catz.core._`:
```scala
import zio.interop.catz.core._
```Note that this library only has an `Optional` dependency on cats-effect – if you or your libraries don't depend on it, this library will not add it to the classpath.
### Example
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` and `cats.effect.Temporal`):
```scala
import zio.{durationInt as _, *}
import zio.interop.catz.*
import doobie.*
import doobie.implicits.*
import scala.concurrent.duration.*object Example extends ZIOAppDefault:
val run = {
val xa: 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
}
```## Documentation
Learn more on the [ZIO Interop Cats homepage](https://zio.dev)!
## Contributing
For the general guidelines, see ZIO [contributor's guide](https://zio.dev/contributor-guidelines).
## Code of Conduct
See the [Code of Conduct](https://zio.dev/code-of-conduct)
## Support
Come chat with us on [![Badge-Discord]][Link-Discord].
[Badge-Discord]: https://img.shields.io/discord/629491597070827530?logo=discord "chat on discord"
[Link-Discord]: https://discord.gg/2ccFBr4 "Discord"## License
[License](LICENSE)