Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Scala have introspection capable of something similar to Python's dir()?

Tags:

Yes, I know it's considered lazy by the non-Pythonistas. The reason I ask is that documentation is still woefully lacking in many Scala libraries (e.g. Scala-dbc, but that's not all I'm looking at), and if I could see the attributes of an object/class at runtime, I could at least figure out what's available. Thanks.

like image 693
JohnMetta Avatar asked Dec 29 '09 06:12

JohnMetta


1 Answers

Scala does not have a reflection API. The only way to access this information is to use the Java reflection API. This has the disadvantage that the structure may change as the way Scala is represented in Java classes and interfaces may change in the future.

scala> classOf[AnyRef].getMethods res0: Array[java.lang.reflect.Method] = Array(public final void ... 

Some specific type information that is present in the byte code can be accessed with the ScalaSigParser.

import tools.scalap.scalax.rules.scalasig._ import scala.runtime._  val scalaSig = ScalaSigParser.parse(classOf[RichDouble]) 
like image 178
Thomas Jung Avatar answered Nov 06 '22 05:11

Thomas Jung