寻找第二个隐式匹配 [英] Finding the second matching implicit

查看:59
本文介绍了寻找第二个隐式匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下设置:

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] {}
}

现在,在REPL(已经加载了上面的代码)中,我可以执行以下操作:

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

是否有一种方法可以使用某种类型级别的魔术进行编码,而我想使用 A =:= Double 跳过实例,但是仍然可以使用类型推断来找出什么 A

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?

我不想遮蔽 foo3 .这是MVCE:在我的真实情况下, foo3 是带有其他隐式参数的 def (并且在派生其他 Foo 时可能起到间接作用的).

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
       ^

推荐答案

肮脏的黑客是将宏上下文转换为它的实现,并使用编译器内部构件.

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

在2.12.8中进行了测试.由 shapeless.Cached .

Tested in 2.12.8. Motivated by shapeless.Cached.

在2.13.0中, materializeSecondFoo.say 应该替换为

In 2.13.0 materializeSecondFoo.say should be replaced with

val m = materializeSecondFoo
m.say

这篇关于寻找第二个隐式匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆