Finding the second matching implicit

☆樱花仙子☆ 提交于 2021-02-10 13:25:40

问题


Consider the following setup:

trait Foo[A]
object Foo extends Priority2

trait Priority0 {
   implicit def foo1: Foo[Int] = new Foo[Int] {}
}
trait Priority1 extends Priority0 {
   implicit def foo2: Foo[Boolean] = new Foo[Boolean] {}
}
trait Priority2 extends Priority1 {
   implicit def foo3: Foo[Double] = new Foo[Double] {}
}

Now, in a REPL (having loaded the above code up), I can do the following:

scala> def implicitlyFoo[A](implicit foo: Foo[A]) = foo
implicitlyFoo: [A](implicit foo: Foo[A])Foo[A]

scala> implicitlyFoo
res1: Foo[Double] = Priority2$$anon$3@79703b86

Is there a way to encode with some typelevel magic that I want to skip over the instances with A =:= Double, but still let type inference figure out what A is?

I do not want to shadow foo3. This is an MVCE: in my real case, foo3 is a def with other implicit arguments (and may play an indirect role in deriving other Foo's).

I've tried =:!= from shapeless but to no avail:

scala> import shapeless._
import shapeless._

scala> def implicitlyFoo2[A](implicit foo: Foo[A], ev: A =:!= Double) = foo
implicitlyFoo2: [A](implicit foo: Foo[A], implicit ev: A =:!= Double)Foo[A]

scala> implicitlyFoo2
<console>:16: error: ambiguous implicit values:
 both method neqAmbig1 in package shapeless of type [A]=> A =:!= A
 and method neqAmbig2 in package shapeless of type [A]=> A =:!= A
 match expected type Double =:!= Double
       implicitlyFoo2
       ^

回答1:


Dirty hack is to downcast macro context to its implemenation and use compiler internals.

  import scala.language.experimental.macros
  import scala.reflect.macros.whitebox

  trait Foo[A] {
    def say: String
  }

  trait Priority0 {
    implicit def foo1: Foo[Int] = new Foo[Int] {
      override def say: String = "int"
    }
  }

  trait Priority1 extends Priority0 {
    implicit def foo2: Foo[Boolean] = new Foo[Boolean] {
      override def say: String = "bool"
    }
  }

  trait Priority2 extends Priority1 {
    implicit def foo3: Foo[Double] = new Foo[Double] {
      override def say: String = "double"
    }
  }

  object Foo extends Priority2


  def materializeSecondFoo[A]: Foo[A] = macro impl

  def impl(c: whitebox.Context): c.Tree = {
    import c.universe._

    val context = c.asInstanceOf[reflect.macros.runtime.Context]
    val global: context.universe.type = context.universe
    val analyzer: global.analyzer.type = global.analyzer

    var infos = List[analyzer.ImplicitInfo]()

    new analyzer.ImplicitSearch(
      tree = EmptyTree.asInstanceOf[global.Tree],
      pt = typeOf[Foo[_]].asInstanceOf[global.Type],
      isView = false,
      context0 = global.typer.context.makeImplicit(reportAmbiguousErrors = false),
      pos0 = c.enclosingPosition.asInstanceOf[global.Position]
    ) {
      override def searchImplicit(
                                   implicitInfoss: List[List[analyzer.ImplicitInfo]],
                                   isLocalToCallsite: Boolean
                                 ): analyzer.SearchResult = {
        val implicitInfos = implicitInfoss.flatten
        if (implicitInfos.nonEmpty) {
          infos = implicitInfos
        }
        super.searchImplicit(implicitInfoss, isLocalToCallsite)
      }
    }.bestImplicit

    val secondBest = infos.tail.head

    global.gen.mkAttributedRef(secondBest.pre, secondBest.sym).asInstanceOf[Tree]
  }

  materializeSecondFoo.say // bool

Tested in 2.12.8. Motivated by shapeless.Cached.


In 2.13.0 materializeSecondFoo.say should be replaced with

val m = materializeSecondFoo
m.say


来源:https://stackoverflow.com/questions/56777174/finding-the-second-matching-implicit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!