Scala: How can I get the class name of a variable in a function?
I would like to do something like def foo[V] : String = { V.getName }
, but of course it is not correct. I am using the name of the class name to locate the file containing the serialized instance of the class on the file system.
+3
Steven noble
source
to share
1 answer
def className[V](implicit ev: ClassTag[V]) : String = ev.runtimeClass.getName()
Using
className[String]
gives java.lang.String
className[Map[String,String]]
gives scala.collection.immutable.Map
+6
Ravi Kiran
source
to share