【发布时间】:2017-04-06 13:57:13
【问题描述】:
假设我有两种类型IntResult 和StringResult:
import cats._
import cats.data._
import cats.implicits._
scala> case class MyError(msg: String)
defined class MyError
scala> type Result[A] = Either[NonEmptyList[MyError], A]
defined type alias Result
scala> type StringResult = Result[String]
defined type alias StringResult
scala> type IntResult = Result[Int]
defined type alias IntResult
现在我想使用map 将IntResult 转换为StringResult:
scala> val good1: IntResult = 10.asRight
good1: IntResult = Right(10)
scala> good1 map (_.toString)
<console>:26: error: type mismatch;
found : good1.type (with underlying type IntResult)
required: ?{def map: ?}
Note that implicit conversions are not applicable because they are ambiguous:
both method catsSyntaxEither in trait EitherSyntax of type [A, B](eab:Either[A,B]) cats.syntax.EitherOps[A,B]
and method toFunctorOps in trait ToFunctorOps of type [F[_], A](target: F[A])(implicit tc: cats.Functor[F])cats.Functor.Ops[F,A]
are possible conversion functions from good1.type to ?{def map: ?}
good1 map (_.toString)
如何解决这种歧义?
【问题讨论】:
-
以上代码在 Scala 2.12.0 中运行良好(但是您的错误发生在 2.11.8 中),升级您的 Scala 是一种选择吗?
-
是的,这可能是一种选择。
-
刚刚遇到了与猫的 Either 仿函数实例完全相同的问题。升级 Scala 对我来说不是一个选择。
-
@Matthias 因为对我来说这是一个选项,我刚搬到
2.12并且它起作用了。也许@oscar-boykin 的答案对你有用。没试过。
标签: scala functor either scala-cats