scala 中的 Groovy 的 classX.metaClass.getProperty

发布于 2024-12-19 17:06:26 字数 198 浏览 5 评论 0原文

scala 中是否有与 Groovy 出色的 SomeClass.metaClass.getProperty 函数等效的东西?这对于制作特定领域的语言非常有帮助。例如,我可以说: val x = SomeClass(); x.任意属性名称,而不是x.get("任意属性名称")x("任意属性名称")

Is there anything equivalent to Groovy's remarkable SomeClass.metaClass.getProperty function in scala? This would be very helpful in making domain specific languages. For example, I could then say: val x = SomeClass(); x.arbitraryPropertyName rather than x.get("arbitraryPropertyName") or x("arbitraryPropertyName").

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜心 2024-12-26 17:06:26

根据您的描述,此功能听起来像 Scala 的 Dynamic。从Scaladoc

支持动态调用的标记特征。此实例 x
Trait 允许为任意方法名称 meth 调用 x.meth(args) 和
参数列表 args。如果 x 本身不支持调用,则它是
重写为 x.applyDynamic("meth", args)。

也就是说,如果您的类扩展了Dynamic,则x.atoryPropertyName形式的调用将被转换为x.applyDynamic("atoryPropertyName"),当然,您还可以定义 applyDynamic 的行为。

From your description, this feature sounds like Scala's Dynamic. From the Scaladoc,

A marker trait that enables dynamic invocations. Instances x of this
trait allow calls x.meth(args) for arbitrary method names meth and
argument lists args. If a call is not natively supported by x, it is
rewritten to x.applyDynamic("meth", args).

That is, if your class extends Dynamic, then calls of the form x.arbitraryPropertyName get translated to x.applyDynamic("arbitraryPropertyName"), and of course you get to define the behavior of applyDynamic.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文