应用于列表中的Cats.sequence错误

Moe*_*ius 6 scala scala-cats

我想转换List[Either[String, Int]]en 的列表Either[String, List[Int]].为此,我想使用Cats.sequence:

/* for ammonite users
interp.load.ivy("org.typelevel" %% "cats-core" % "1.0.1")

@
*/

import cats.instances.list._
import cats.instances.either._
import cats.syntax.traverse._

val seqCorrect: List[Either[String, Int]] = List(Right(1), Right(2), Right(3))

val result1 = seqCorrect.sequence
Run Code Online (Sandbox Code Playgroud)

但是我有以下错误:

Cannot prove that Either[String,Int] <:< G[A].
val result1 = seqCorrect.sequence
                         ^
Run Code Online (Sandbox Code Playgroud)

我发现错误消息非常无益.我该如何解决这个问题?

Luk*_*itz 6

我想你可能没有启用partial-unification.最简单的方法是添加sbt-partial-unification 插件.

如果你使用的是Scala 2.11.9或更高版本,你也可以简单地添加编译器标志:

scalacOptions += "-Ypartial-unification"
Run Code Online (Sandbox Code Playgroud)

  • 如果您使用的是ammonite,可以使用`interp.configureCompiler(_.settings.YpartialUnification.value = true)启用它. (3认同)