本文介绍了找出隐式调用链的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么办法可以找出整个隐式链(我对所有隐式类型都感兴趣).我正在使用 IntelliJ Idea,但我正在寻找任何方法来做到这一点,即使我必须使用另一个 IDE.(我想知道 REPL 是否可以帮助我)

Is there any way how can I figure out the whole implicit chain (and I am interested in all of the implicit kinds). I'm using IntelliJ Idea, but I'm searching for any way to do that, even if I have to work with another IDE. (and I'm wondering whether REPL can help me with that)

例如我写 a gt b 其中 gt 来自 scalaz.我想知道:

For example I write a gt b where gt comes from scalaz. And I want to know:

  1. 究竟使用了哪个 Order 的隐式实例
  2. 使用了什么类型类(我知道在这个特定实例中的答案 - 在 scalaz 中很容易,但通常有时并不总是那么明显)
  3. a 如何接收一个方法 gt 的整个链.对于这个特定示例,我知道使用了 ToOrderOps 特性,但总的来说,我可能不知道这一点,而且我也无法弄清楚 ToOrderOps 是如何导入的.立>
  1. Exactly what implicit instance of Order was used
  2. What typeclass was used (I know the answer in this particular instance - it's easy in scalaz, but in general sometimes it not always that obvious)
  3. Whole chain how a received a method gt. For this particular example, I know that ToOrderOps trait was used, but in general I may not know that and I also can't figure out how ToOrderOps was imported.

推荐答案

在 REPL 中使用 Scala 反射 API 通常是开始此类调查的好方法:

Using the Scala reflection API in the REPL is usually a good way to start this kind of investigation:

scala> import scala.reflect.runtime.universe.reify
import scala.reflect.runtime.universe.reify

scala> import scalaz._, Scalaz._
import scalaz._
import Scalaz._

scala> println(reify(1 gt 2))
Expr[Boolean](Scalaz.ToOrderOps(1)(Scalaz.intInstance).gt(2))

scala> println(reify("a" gt "b"))
Expr[Boolean](Scalaz.ToOrderOps("a")(Scalaz.stringInstance).gt("b"))

这里的 ToOrderOps 是一种方法,而不是特征,并且 Scalaz 表示您正在看到它,因为 scalaz.Scalaz 混合了在 ToOrderOps 特征中,所以我认为这种方法解决了您的所有三个问题.

The ToOrderOps here is a method, not the trait, and the Scalaz indicates that you're seeing it because scalaz.Scalaz mixes in the ToOrderOps trait, so I think this approach addresses all three of your points.

这篇关于找出隐式调用链的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-22 08:05