Find all implicits
Asked Answered
P

2

17

Is there a way in Scala to find and display all uses of implicits in my code, or compile without implicits enabled (so I get compilation errors)? I'm hoping to identify places where I'm unintentionally allocating extra memory in inner loops.

Puglia answered 31/12, 2011 at 0:48 Comment(0)
R
18

You can use -Xprint:typer option and look at how the inner loops of interest are typed checked. The implicit calls will appear in the printed source code. You may need to pipe the output to a file so that you can search for the interesting sections.

Also the Scala Eclipse IDE nightlies at http://scala-ide.org/download/nightly.html have a display implicit feature. You have to enable it in the Preferences, Scala, Implicits section.

The double arrows on the left margin shows that an implicit is used:

enter image description here

On hover, it shows the details:

enter image description here

The IDEA scala plugin can also show implicit conversions with underline, see the same piece of code:

enter image description here

It should also show which one is used using the CTRL-SHIFT-Q shortcut, but I had less success with it...

Recombination answered 31/12, 2011 at 5:3 Comment(0)
T
12

As of yesterday (it's just a coincidence) there is -Xlog-implicit-conversions.

scalac -Xlog-implicit-conversions -d /tmp /scala/trunk/src/library/scala/util/Random.scala 
/scala/trunk/src/library/scala/util/Random.scala:115: applied implicit conversion from Int to ?{val to: ?} = implicit def intWrapper(x: Int): scala.runtime.RichInt
    for (n <- buf.length to 2 by -1) {
                  ^
Tristram answered 3/1, 2012 at 19:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.