简体   繁体   中英

Cats Monad Transformers

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.

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)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM