【发布时间】:2018-11-20 14:49:23
【问题描述】:
如何将sequence 函数用于带有自定义类 CustomObj 的 List[EitherT[Future, String, CustomObj]] ?我想要这样的东西:
import scala.language.postfixOps
import cats.instances.list._
import cats.syntax.traverse._
import cats.data.EitherT
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
case class CustomObj(int: Int)
private val fst: EitherT[Future, String, CustomObj] =
EitherT.pure[Future, String](CustomObj(1))
private val snd: EitherT[Future, String, CustomObj] =
EitherT.pure[Future, String](CustomObj(2))
val source: List[EitherT[Future, String, CustomObj]] = fst :: snd :: Nil
val result: EitherT[Future, String, List[CustomObj]] = source.sequence
import scala.concurrent.duration._
val res = scala.concurrent.Await.result(result.value, 1 second)
println(res) // Right(List(CustomObj(1), CustomObj(2)))
每次编译时我都会得到
error: Cannot prove that EitherT[Future,String,CustomObj] <:< G[A].
什么意思 <: g>
【问题讨论】:
-
@erip A note on sequencing
-
“不起作用”是什么意思?
-
我得到:错误:无法证明cats.data.EitherT[Future, SomeType1, SomeType2] <: g>
-
您应该创建一个我们可以复制和粘贴的最小示例。如果没有更多信息,我无法调试。
-
scalacOptions += "-Ypartial-unification" 。如果您使用 maven,只需将其添加到 build.sbt 或 pom.xml。这可以解决问题。现在一切正常。感谢您的 cmets。
标签: scala scala-cats either