Reputation: 42110
Suppose I've got two types IntResult
and 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
Now I would like to convert IntResult
to StringResult
using map
:
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)
How to resolve this ambiguity ?
Upvotes: 1
Views: 473
Reputation: 1974
The simple approach is to use the projections, so you do:
good1.right.map(_.toString)
It's not awesome, but it works. I don't know a way to import so that the functor is not conflicting with the either enrichment (if you need both).
Of course, you could just import cats.syntax.either._
but that won't get you the Functor.
Upvotes: 1