【发布时间】:2018-01-14 08:34:48
【问题描述】:
我想将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?
【问题讨论】: