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

前端 未结 4 601
半阙折子戏
半阙折子戏 2020-12-25 13:10

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\

4条回答
  •  滥情空心
    2020-12-25 14:09

    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])
    

提交回复
热议问题