Scalaz:如何将 EitherInstances 与 MonadError 一起使用?

Scalaz: How to use EitherInstances with MonadError?

我想将 scala.util.Eitherscalaz.MonadError 一起使用,但我 运行 遇到了一些类型问题。 我最简单的失败代码如下:

object Foo extends EitherInstances {
  private val success: Either[String, Int] = Right(42)

  def main(args: Array[String]): Unit = {
    MonadError.apply(eitherMonad[String]).handleError[Int](success)(_ => Right(43))
  }
}

但是,这会导致以下无法辨认的错误:

Error:(10, 16) no type parameters for method apply: (implicit F: scalaz.MonadError[F,S])scalaz.MonadError[F,S] in object MonadError exist so that it can be applied to arguments (scalaz.Traverse[[β[=11=]$]scala.util.Either[String,β[=11=]$]] with scalaz.MonadError[[β$]scala.util.Either[String,β$],String] with scalaz.BindRec[[β$]scala.util.Either[String,β$]] with scalaz.Cozip[[β$]scala.util.Either[String,β$]])
 --- because ---
argument expression's type is not compatible with formal parameter type;
 found   : scalaz.Traverse[[β[=11=]$]scala.util.Either[String,β[=11=]$]] with scalaz.MonadError[[β$]scala.util.Either[String,β$],String] with scalaz.BindRec[[β$]scala.util.Either[String,β$]] with scalaz.Cozip[[β$]scala.util.Either[String,β$]]
 required: scalaz.MonadError[?F,?S]
    MonadError.apply(eitherMonad[String]).handleError[Int](success)(_ => Right(43))
Error:(10, 33) type mismatch;
 found   : scalaz.Traverse[[β[=11=]$]scala.util.Either[String,β[=11=]$]] with scalaz.MonadError[[β$]scala.util.Either[String,β$],String] with scalaz.BindRec[[β$]scala.util.Either[String,β$]] with scalaz.Cozip[[β$]scala.util.Either[String,β$]]
 required: scalaz.MonadError[F,S]
    MonadError.apply(eitherMonad[String]).handleError[Int](success)(_ => Right(43))

如果我尝试让编译器推断类型,例如,在以下代码中:

object Foo extends EitherInstances {
  private val success: Either[String, Int] = Right(42)

  def main(args: Array[String]): Unit = {
    MonadError.apply.handleError(success)(_ => Right(43))
  }
}

我收到以下错误:

Error:(11, 16) could not find implicit value for parameter F: scalaz.MonadError[F,S]
MonadError.apply.handleError(success)(_ => Right(43))
Error:(11, 16) not enough arguments for method apply: (implicit F: scalaz.MonadError[F,S])scalaz.MonadError[F,S] in object MonadError.
Unspecified value parameter F.
    MonadError.apply.handleError(success)(_ => Right(43))

由于 eitherMonad 的结果明确声明自己是 MonadError,我假设我在这里做错了什么。我需要做什么才能说服编译器 Either 确实是 MonadError?

I'm assuming I'm doing something wrong here

您需要启用 Ypartial-unification,代码将在不做任何更改的情况下编译。

sbt:

scalacOptions in Compile ++= Seq("-Ypartial-unification")

您还可以使用 kind-projector 为类型 lambda 设置别名并帮助编译器:

sbt:

addCompilerPlugin("org.spire-math" %% "kind-projector" % "0.9.4")

然后:

MonadError.apply[Either[String, ?], String].handleError(success)(_ => Right(43))