Scalaz 7.2 Integration

Monix provides integration with Scalaz library, implementing useful type-classes for the data types exposed.

Adding the Optional Dependency

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

libraryDependencies ++= Seq(
  "io.monix" %% "monix" % "2.2.2",
  "io.monix" %% "monix-scalaz-72" % "2.2.2"
)

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

libraryDependencies ++= Seq(
  "io.monix" %% "monix-eval" % "2.2.2",
  "io.monix" %% "monix-scalaz-72" % "2.2.2"
)

For more details, see Usage in SBT and Maven.

Usage

You just need to import the defined implicits:

import monix.scalaz._

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

scala> import scalaz.Monad
import scalaz.Monad

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

scala> implicitly[Monad[Task]]
res0: scalaz.Monad[monix.eval.Task] = monix.scalaz.MonixToScalaz10$MonixToScalazBindRec@40c2b442

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

import monix.scalaz.monixToScalazMonad

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

import monix.scalaz.reverse._

import scalaz._
import scalaz.std.AllInstances._

Here is a reverse conversion in action, pulling a monix.types.Monad[List] out of the instances defined for Scalaz:

implicitly[monix.types.Monad[List]]
// res1: monix.types.Monad[List] = monix.scalaz.ScalazToMonix2$$anon$4@67c0a0c7

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

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

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