IntelliJ shows type mismatch errors about dependent method types, even though sbt compiles fine (scala)

1.4k views Asked by At

I have a trait like following:

trait NumberRepository[C <: AppContext] {
  def findAll(implicit ctx: C): ctx.Result[Seq[Int]]
  def findEvens(implicit ctx: C): ctx.Result[Seq[Int]] = findAll.map(_.filter(_ % 2 == 0))
}

It can be compiled without any errors when I use SBT. But my IntelliJ reports a type mismatch error about the return value of findEvens on its editor.

Type mismatch.
Required: ctx.Result[scala.Seq[Int]]
Found: C#Result[scala.collection.Seq[Int]]

I've found that I can work around the problem by passing 'ctx' param to findAll explicitly. But I don't want to do that for some reasons.

// it's OK!
def findEvens(implicit ctx: C): ctx.Result[Seq[Int]] = findAll(ctx).map(_.filter(_ % 2 == 0))

Is it a bug? Or something wrong with my code or configurations? Is there any way to fix this error?

AppContext is like following:

trait AppContext {
  type Result[+A] <: AppResult[Result, A]
  def success[A](a: A): Result[A]
}

trait AppResult[F[+_], +A] {
  def map[B](f: A => B): F[B]
  def flatMap[B](f: A => F[B]): F[B]
}

class AppContextImpl extends AppContext {
  type Result[+A] = AppResultImpl[A]
  override def success[A](a: A): AppResultImpl[A] = AppResultImpl(Some(a))
}

case class AppResultImpl[+A](value: Option[A]) extends AppResult[AppResultImpl, A] {
  override def map[B](f: A => B): AppResultImpl[B] = AppResultImpl(value.map(f))
  override def flatMap[B](f: A => AppResultImpl[B]): AppResultImpl[B] = value match {
    case Some(a) => f(a)
    case None => AppResultImpl(None)
  }
}

class NumberRepositoryImpl extends NumberRepository[AppContextImpl] {
  override def findAll(implicit ctx: AppContextImpl): AppResultImpl[Seq[Int]] = ctx.success(1 to 10)
}
1

There are 1 answers

1
Mario Galic On BEST ANSWER

Scala type-aware highlighting is an IntelliJ feature separate from Scala compiler proper so it might sometimes behave differently. False errors may reported

To help us to fix a highlighting glitch you may report it to YouTrack as usual or by pressing Alt+Enter on wrong highlight:

enter image description here

Also type-aware highlighting can be disabled/enabled via the small T icon at bottom right

enter image description here

Alternatively try getting error diagnostics from BSP which should be 100% authentic but perhaps less featureful.