Scala 3 - mixing implicit classes and extensions

Viewed 54

I am in process of porting a large project from Scala 2.13 to Scala 3. Until the process is complete, I still need the project to compile with Scala 2.13, therefore I cannot remove all implicits in favour of extensions and similar Scala 3 features.

I am experiencing an obstacle now which I have no idea how to deal with: I have an at method provided as a Scala 2 extension (using implicit class), but there is also an at method provided by Quicklens library, which, being already ported to Scala 3, is a proper Scala 3 extension.

Now the trouble is the compiler does not see my at method, even if the extension is not applicable.

object AtGiven {

  class F[T]

  trait Functor[F[_]]

  extension[F[_] : [G[_]] =>> Functor[G], A] (fa: F[A]) {
    def at(idx: Int): A = ???
  }

  val f = new F[Unit]

  implicit class FOps[T](f: F[T]) {
    def at(i: Int) = i
  }

  def main(args: Array[String]): Unit = {
    val fi = f.at(0)
  }
}

When I replace the implicit class with following extension, the code works fine:

  extension [T](fa: F[T])
    def at(i: Int) = i

This is however not a solution at this moment, because such code does not compile with Scala 2.13.

Are there any rules regarding extension vs. implicit class resolution priorities?

It this a Scala 3 compiler bug, feature, or limitation? How can I work around it?

0 Answers
Related