Mun*_* Ar 2 types scala sequence applicative scala-cats
我有一个类型的值List[EitherT[IO, String, Int]],我想对其进行序列以便将其映射到EitherT[IO,String, List[Int]]
我阅读并找到了序列方法,但它给了我一个错误,说它需要 [G] 的隐式应用,如何解决这个问题
如果没有MCVE ,很难猜测编译错误的原因(“需要隐式Applicative[G]”)。请提供一份。
在 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
为什么导入类型类实例不再需要 import cats.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._
Run Code Online (Sandbox Code Playgroud)
从 Cats 2.2.0 开始,您仍然需要导入语法,但不再需要导入实例。
在 Scala 2.13.0 之前,您必须添加build.sbt
scalacOptions += "-Ypartial-unification"
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
或者你可以拆开包装EitherT
EitherT(
l1.traverse(_.value)
.map(
_.sequence[Either[String, *], Int]
)
)
Run Code Online (Sandbox Code Playgroud)
如果您编写一个通用方法,也许添加上下文绑定就足够了
def foo[G[_]: Applicative] = {
// ^^^^^^^^^^^ here
// ... using .sequence ...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1554 次 |
| 最近记录: |