What Scala version, what Cats version? Works for me with
scalaVersion := "2.13.4"
libraryDependencies += "org.typelevel" %% "cats-core" % "2.3.0"
And
scala> sealed trait Error { def str: String }; object SomeError extends Error { def str = "" }
trait Error
object SomeError
scala> import cats.implicits._
import cats.implicits._
scala> val a: Either[Error, Int] = SomeError.asLeft[Int]
val a: Either[Error,Int] = Left(SomeError$@2b90ba50)
As an aside, note that Scala 2.13 added .withLeft
and .withRight
methods to Either
, so you can do the equivalent without involving Cats at all:
scala> val a: Either[Error, Int] = Left(SomeError).withRight[Int]
val a: Either[Error,Int] = Left(SomeError$@7fbccfbb)
You might prefer asLeft
and asRight
on stylistic grounds, I suppose, but I doubt they would ever have been added to Cats if the stdlib methods had existed all along.