In the REPL there’s a command to print the type:
scala> val s = "House"
scala> import scala.reflect.runtime.universe._
scala> val li = typeOf[List[Int]]
scala> :type s
String
scala> :type li
reflect.runtime.universe.Type
How can I get this “:type expr” functionality in my Scala program to print types?
Let me clarify the “:type expr” functionality I would like to have, something like this:
println(s.colonType) // String
println(li.colonType) // reflect.runtime.universe.Type
How can I get such a “colonType” method in my Scala program outside the REPL (where I don’t have the :type command available)?
The following implicit conversion should do the trick for you: