且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Scala是否具有类似于Python的dir()的自省功能?

更新时间:2021-08-11 03:20:26

Scala没有反射API.访问此信息的唯一方法是使用 Java反射API .这样做的缺点是,将来随着Java类中Scala的表示方式和接口的变化,结构可能会发生变化.

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 ...

可以使用 ScalaSigParser 访问字节码中存在的某些特定类型信息. /p>

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