John Zeng
John Zeng

Reputation: 1224

How can I list all useable implicit conversion?

I know that in SCALA I can convert a type to another by define some implicit function, my question is , how can I know what conversions can I use after importing tons of packages?

For example, I have a string, and than how can I know what types can it convert to ?

Edit to clarify, I wanna do it in my scala compile plugin, so I may need to call a function on a reflect.api.tree type, and then get the implicits. I am looking some method to use the implicitly[] mentioned in the answer.

Upvotes: 5

Views: 561

Answers (3)

John Zeng
John Zeng

Reputation: 1224

Although I accepted jwvh's answer but his answer can only work for normal user. I fond a tricky way to do it in a compiler plugin, the code is not for common usage so I don't paste it here, if any of you are interesting about how to get all implicit values in compiler plugin, you can refer to my githubScala-completion-Base

Upvotes: 0

jwvh
jwvh

Reputation: 51271

In the REPL you can invoke :implicits to see all the in-scope implicits other than those available from the Predef. (Add -v to see Predef implicits as well.)

You can also invoke the implicitly[] function from anywhere in your code to test for particular implicits.

scala> implicitly[String => Seq[Char]]
res0: String => Seq[Char] = <function1>

scala> implicitly[String => Array[Char]]
<console>:12: error: No implicit view available from String => Array[Char].
       implicitly[String => Array[Char]]
                 ^

Upvotes: 6

chengpohi
chengpohi

Reputation: 14227

In scala repl, you can use :implicits -v to list all implicits under this context, like:

/* 69 implicit members imported from scala.Predef */
  /* 7 inherited from scala */
  final implicit class ArrayCharSequence extends CharSequence
  final implicit class ArrowAssoc[A] extends AnyVal
  final implicit class Ensuring[A] extends AnyVal
  final implicit class RichException extends AnyVal
  final implicit class SeqCharSequence extends CharSequence
  final implicit class StringFormat[A] extends AnyVal
  final implicit class any2stringadd[A] extends AnyVal
  ...

Upvotes: 3

Related Questions