Scala是否具有类似于Python的dir()的自省功能? [英] Does Scala have introspection capable of something similar to Python's dir()?

查看:103
本文介绍了Scala是否具有类似于Python的dir()的自省功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是的,我知道非Pythonista用户认为它是懒惰的.我问的原因是很多Scala库(例如Scala-dbc,但我所要看的不是)仍然严重缺乏文档,如果我能在运行时看到对象/类的属性,我可以最少弄清楚可用的东西.谢谢.

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.

推荐答案

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

这篇关于Scala是否具有类似于Python的dir()的自省功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆