我有一個型別的值,List[EitherT[IO, String, Int]]
我想對其進行序列處理以將其映射到EitherT[IO,String, List[Int]]
我閱讀并找到了序列方法,但它給了我一個錯誤,說它需要 [G] 的隱式應用程式如何解決這個問題
uj5u.com熱心網友回復:
如果Applicative[G]
沒有MCVE ,很難猜測編譯錯誤的原因(“需要一個隱式”)。請提供一份。
在 Cats 2.2.0 之前需要匯入實體
https://meta.plasm.us/posts/2019/09/30/implicit-scope-and-cats/
https://github.com/typelevel/cats/releases/tag/v2.2.0
http://eed3si9n.com/herding-cats/import-guide.html
為什么匯入型別類實體不再需要 importcats.implicits._ ?
import cats.instances.either._ // or cats.instances.all._ import cats.instances.list._ // or cats.instances.all._ import cats.syntax.traverse._ // or cats.syntax.all._ // or cats.implicits._
從 Cats 2.2.0 開始,您仍然需要匯入語法,但不再需要匯入實體。
在 Scala 2.13.0 之前,您必須添加到
build.sbt
scalacOptions = "-Ypartial-unification"
https://github.com/typelevel/cats/issues/2948
http://eed3si9n.com/herding-cats/partial-unification.html
值 YpartialUnification 不是 scala.tools.nsc.Settings 的成員
https://www.reddit.com/r/scala/comments/7ak9c5/ypartialunification/
或者您可能需要指定泛型(不是推斷的)
val l: List[Either[String, Int]] = ??? val l1: List[EitherT[IO, String, Int]] = ??? l.sequence[({ type G[X] = Either[String, X] })#G, Int] // using type lambda l1.sequence[({ type G[X] = EitherT[IO, String, X] })#G, Int] // using type lambda l.sequence[Either[String, *], Int] // using kind-projector l1.sequence[EitherT[IO, String, *], Int] // using kind-projector
或者你可以 unwrap-wrap
EitherT
EitherT( l1.traverse(_.value) .map( _.sequence[Either[String, *], Int] ) )
如果您撰寫通用方法,則添加背景關系系結就足夠了
def foo[G[_]: Applicative] = { // ^^^^^^^^^^^ here // ... using .sequence ... }
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/318742.html