我想将scala.util.Either
与scalaz.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[[β$0$]scala.util.Either[String,β$0$]] with scalaz.MonadError[[β$1$]scala.util.Either[String,β$1$],String] with scalaz.BindRec[[β$2$]scala.util.Either[String,β$2$]] with scalaz.Cozip[[β$3$]scala.util.Either[String,β$3$]])
--- because ---
argument expression's type is not compatible with formal parameter type;
found : scalaz.Traverse[[β$0$]scala.util.Either[String,β$0$]] with scalaz.MonadError[[β$1$]scala.util.Either[String,β$1$],String] with scalaz.BindRec[[β$2$]scala.util.Either[String,β$2$]] with scalaz.Cozip[[β$3$]scala.util.Either[String,β$3$]]
required: scalaz.MonadError[?F,?S]
MonadError.apply(eitherMonad[String]).handleError[Int](success)(_ => Right(43))
Error:(10, 33) type mismatch;
found : scalaz.Traverse[[β$0$]scala.util.Either[String,β$0$]] with scalaz.MonadError[[β$1$]scala.util.Either[String,β$1$],String] with scalaz.BindRec[[β$2$]scala.util.Either[String,β$2$]] with scalaz.Cozip[[β$3$]scala.util.Either[String,β$3$]]
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
?
答案 0 :(得分:2)
我假设我在这里做错了什么
您需要启用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))