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)
}
Scala type-aware highlighting is an IntelliJ feature separate from Scala compiler proper so it might sometimes behave differently. False errors may reported
Also type-aware highlighting can be disabled/enabled via the small
T
icon at bottom rightAlternatively try getting error diagnostics from BSP which should be 100% authentic but perhaps less featureful.