1

I've made use of a few of scala's built-in type classes, and created a few of my own. However, the biggest issue I have with them at the moment is: how do I find type classes available to me? While most of those that I write are small and simple, it would be nice to know if something already exists that does what I'm about to implement!

So, is there a list, somewhere, of all the type classes or implicit values available in the standard library? Even better, is it possible to somehow (probably within the REPL) generate a list of the implicit values available in the current scope?

Micheal Hill
  • 1,619
  • 2
  • 17
  • 38
  • Only one implicit is resolved, otherwise the compiler would raise an error. In macro you can check and get the matching macro. – cchantep May 15 '15 at 11:22
  • I understand that. I don't want an implicit for some specific case; I want to know what _all_ of the possible implicits available to me are. – Micheal Hill May 15 '15 at 12:36
  • Don't think that possible. What would be the point? – cchantep May 15 '15 at 20:07
  • 1
    Check out Scala Plugin 1.4.x for IntelliJ IDEA 14 & 14.1: http://blog.jetbrains.com/scala/2015/03/26/what-to-check-out-in-scala-plugin-1-4-x-for-intellij-idea-14-14-1/ – mirelon May 16 '15 at 07:16
  • @cchantep Why do you say it would be impossible? When the compiler searches for implicits in some scope, the search is both deterministic and finite. Could a program not follow similar logic as the compiler would here and just print out everything it finds? There are two points; to know what type classes are already available to me (to use something provided, rather than rewriting the same thing) and to see how a similar type class is implemented, so I can better implement my own. The documentation doesn't seem to provide any easy way to do either of these without knowing of the class first! – Micheal Hill May 17 '15 at 06:09
  • The compiler is doing a lot of complicated thingy that a program is not able to do itself. – cchantep May 17 '15 at 10:31
  • What complicated things? Why is an arbitrary program not able to them? The compiler is itself a program, after all. Certainly, a program that is running wouldn't necessarily be able to inspect itself in this way (depending on the language, etc.), but I wasn't meaning to suggest that the inspections I want to do are within the running program. I effectively wanted to pass my code and a line number to some program and have it give me a list of implicits. That's what my question was asking, and more-or-less what is demonstrated in the answer below. – Micheal Hill May 18 '15 at 03:26

1 Answers1

1

It's a job for a good IDE.

IntellijIDEA 14+

Check out Implicits analyser in Scala Plugin 1.4.x. Example usage:

def myMethod(implicit a: Int) = {
}
implicit val a: Int = 1
myMethod // click the myMethod and press Ctrl+Shift+P, the "Implicit Parameters" is shown

Eclipse

Check out Implicit highlighting.

Scala REPL

You can list implicits like this:

:implicits -v

And investigate their origin like defined here:

import reflect.runtime.universe
val tree = universe.reify(1 to 4).tree
universe.showRaw(tree)
universe.show(tree)
Community
  • 1
  • 1
mirelon
  • 4,896
  • 6
  • 40
  • 70
  • That's exactly what I was looking for from the REPL, but how do you access the analyser in IntelliJ? I tried searching for a command relating to implicits, but none of those available to me match the screenshot in that link you provided. – Micheal Hill May 17 '15 at 05:58