Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Figuring out chain of implicit invocations

Tags:

scala

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)

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

  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.
like image 977
Archeg Avatar asked Jan 20 '16 15:01

Archeg


1 Answers

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"))

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.

like image 59
Travis Brown Avatar answered Oct 17 '22 21:10

Travis Brown