简体   繁体   English

猫Monad变形金刚

[英]Cats Monad Transformers

I am trying to learn how to nest monads using MonadTransformers in cats library. 我正在尝试学习如何在猫库中使用MonadTransformers嵌套Monad。

So I am trying to build a data type for Either[String, Option[A]] 因此,我正在尝试为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. 我从使用Xor的样本中获取了此信息,但我想最新的cats库将Xor删除,转而支持Either。

I suspect that you are missing an import of cats instance: 我怀疑您缺少进口的cats实例:

import cats.instances.either._

(Also the import of import cats.instances.list._ seems to be superfluous here.) (此外, import cats.instances.list._import cats.instances.list._在这里似乎是多余的。)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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