How to find implicit function or variables in scala

I cannot find the implicit conversions or implicit value values ​​used in Scala code. This makes reading open source projects very confusing.

An implicitly triple can only help check if a valid implicit variable of any type exists, but it was not possible to determine where the implicit variable was defined.

 scala> implicitly[(Int) => RichInt] res2: Int => scala.runtime.RichInt = <function1> 

Is there an easy way to find definitions of implicit conversions and values ​​used by a piece of code? If the source code file is very long, it will be a huge job.

+7
scala implicit conversion
source share
2 answers

One trick that has been reviewed on SO:

 scala> import reflect.runtime._,universe._ import reflect.runtime._ import universe._ scala> reify { "abc".size } res1: reflect.runtime.universe.Expr[Int] = Expr[Int](Predef.augmentString("abc").size) 

Recent REPL:

 scala> 3 until 4 //print<hit tab completion> scala.Predef.intWrapper(3).until(4) // : scala.collection.immutable.Range 
+9
source share

You can also use the Scala plugin for IntelliJ IDEA. Implicit conversions are emphasized as follows (where Int 1 converted to BigInt ):

enter image description here

The above example is taken from https://www.jetbrains.com/help/idea/2016.1/working-with-scala-implicit-conversions.html?origin=old_help#highlight . You can read more on this page.

0
source share

All Articles