如何对自定义类CustomObj的List [EitherT [Future,String,CustomObj]]使用sequence
函数?我想要这样的东西:
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].
是什么意思<:
答案 0 :(得分:0)
这是一个经过编辑的版本,代码为https://scastie.scala-lang.org/Yaneeve/Jro89ZHwS3G23Aveanxc5A:
import scala.language.postfixOps
import cats.implicits._
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)))
我最初写道:
请注意,它没有编译并产生了奇怪的编译消息 直到我添加
import scala.concurrent.ExecutionContext.Implicits.global
确实如此, 但是 ,您的麻烦并不在那里。当我再次进行复制时,出现了您指定的错误。缺少的是一个SBT / scalac标志:scalacOptions += "-Ypartial-unification"