Typelevel Cats Integration

Monix provides integration with Typelevel’s Cats library, implementing useful type-classes for the data types exposed.

Adding the Optional Dependency

The monix-cats is an optional dependency that you can add alongside the main Monix dependency. So in build.sbt:

libraryDependencies ++= Seq(
  "io.monix" %% "monix" % "2.2.4",
  "io.monix" %% "monix-cats" % "2.2.4"
)

Or maybe you want to use just Task along with this integration:

libraryDependencies ++= Seq(
  "io.monix" %% "monix-eval" % "2.2.4",
  "io.monix" %% "monix-cats" % "2.2.4"
)

For more details, see Usage in SBT and Maven.

Usage

You just need to import the defined implicits:

import monix.cats._

This will bring Monix’s type-class instances in scope. And now we can verify that Task is indeed a cats.Monad:

scala> import cats.Monad
import cats.Monad

scala> import monix.eval.Task
import monix.eval.Task

scala> implicitly[Monad[Task]]
res0: cats.Monad[monix.eval.Task] = monix.cats.MonixToCatsCore11$MonixToCatsMonadRec@3abad69a

In case you want a more à la carte importing experience, that’s possible as well:

import monix.cats.monixToCatsMonad

What these imports are doing is to convert the types defined in monix.types to Cats’ type-classes. However the convertion can also work in reverse:

import monix.cats.reverse._

Here is a reverse conversion in action, pulling a monix.types.Monad[cats.Eval]:

implicitly[monix.types.Monad[cats.Eval]]
// res0: monix.types.Monad[cats.Eval] = monix.cats.CatsCoreToMonix2$$anon$4@4d8c2a96

Note that having both of these wildcard imports in scope generates problems, so avoid doing it:

// DO NOT DO THIS!
import monix.cats._
import monix.cats.reverse._

That’s it, now you can use the Cats awesome type-classes along with Monix’s abstractions, such as Coeval, Task and Observable.