Type inference not working as intended

Why do I get the error mentioned below?

sealed trait Error {
  def str: String
}

object SomeError extends Error {
  def str: String
}

//in another package
imports cats.implicits._
val a: Either[Error, Int] = SomeError.asLeft[Int] // compilation fails "expected Either[SomeError.type, Int]"

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.

2 Likes

Scala version used is 2.13.2. I have not imported cats. I am using http4s and circe. They ship with cats-core. I ran dependencyTree and found that circe used cats 2.1.0 and others use cats 2.1.1.
Is there a way to know which version my code is picking?

1 Like

My bad, It was picking Error from package object scala which happens to be a type alias for java.lang.Error. Apologies for taking your time on this

3 Likes

Aha! No worries, glad you sorted it out.

2 Likes

show dependencyClasspath is one way.

2 Likes