views:

411

answers:

3

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.

+5  A: 

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])
Thomas Jung
+2  A: 

That's one of my main uses for REPL. Type the object's name, dot, and then TAB and it will show all available methods.

It isn't perfect. For one thing, it shows protected methods, which won't be available unless you are extending the class. For another thing, it doesn't show methods available through implicit conversion.

And, of course, the IDEs are all capable of doing that.

Daniel
Yeah, that's my initial solution, but in Intellij IDEA 9 (using Scala 2.7.7) I don't get enough information from the scala.dbc class, specifically, when building a query (for which I really need documentation more than introspection, but this was a last resort).
JohnMetta
+2  A: 

You might want something like the following which would give you what you need. In this case, it operates on a String, obviously.

val testStr = "Panda"
testStr.getClass.getMethods.foreach(println)

Does that work?

yonkeltron