Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to find the implicit function or variables in scala

I can't find the implicit conversions or implicit argument values being used in Scala code. This makes reading open source projects very confusing.

The implicitly trick can only help to check if there is a valid implicit variable of some type, but couldn't tell where the implicit variable was defined.

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

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

like image 344
严孝伟 Avatar asked Mar 25 '16 05:03

严孝伟


People also ask

What are implicit variables in Scala?

Implicit parameters are the parameters that are passed to a function with implicit keyword in Scala, which means the values will be taken from the context in which they are called.

What is an implicit function in Scala?

Scala implicit allows you to omit calling method or parameter directly. For example, you can write a function that converts int to/from string explicitly but you can ask the compiler to do the same thing for you, implicitly.

What does implicit Val mean in Scala?

The implicit system in Scala allows the compiler to adjust code using a well-defined lookup mechanism. A programmer in Scala can leave out information that the compiler will attempt to infer at compile time. The Scala compiler can infer one of two situations: A method call or constructor with a missing parameter.

What is implicit parameter?

The implicit parameter in Java is the object that the method belongs to. It's passed by specifying the reference or variable of the object before the name of the method. An implicit parameter is opposite to an explicit parameter, which is passed when specifying the parameter in the parenthesis of a method call.


1 Answers

One trick that has been covered 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
like image 99
som-snytt Avatar answered Oct 09 '22 15:10

som-snytt