Cats Monad Transformers

1k views Asked by At

I am trying to learn how to nest monads using MonadTransformers in cats library.

So I am trying to build a data type for Either[String, Option[A]]

This is the code which I have written

import cats.data.OptionT
import cats.instances.list._
import cats.syntax.applicative._

object Ex11 extends App {
   type ErrorEither[A] = Either[String, A]
   type ErrorOrOption[A] = OptionT[ErrorEither, A]
   val x = 42.pure[ErrorOrOption]
   println(x)
}

But I get an error

[error] Ex11.scala:13: could not find implicit value for parameter F: cats.Applicative[Ex11.ErrorOrOption]
[error]    val x = 42.pure[ErrorOrOption]
[error]                   ^

I took this from a sample which was using Xor but I guess the latest cats library removed Xor in favor of Either.

1

There are 1 answers

0
Arseniy Zhizhelev On BEST ANSWER

I suspect that you are missing an import of cats instance:

import cats.instances.either._

(Also the import of import cats.instances.list._ seems to be superfluous here.)

The following should compile:

import cats.data.OptionT
import cats.instances.either._
import cats.syntax.applicative._

object Ex11 extends App {
   type ErrorEither[A] = Either[String, A]
   type ErrorOrOption[A] = OptionT[ErrorEither, A]
   val x = 42.pure[ErrorOrOption]
   println(x)
}