Mirah 提供哪些元编程功能?
Mirah 主页说
Mirah 支持各种编译时元编程和宏机制。动态语言的大部分“公开课”感觉在 Mirah 中都是可能的。
但我无法找到任何具体细节。有人有进一步的信息吗?
The Mirah home page says
Mirah supports various mechanisms for compile-time metaprogramming and macros. Much of the “open class” feel of dynamic languages is possible in Mirah.
But I'm unable to find any specifics. Does anyone have further information?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Mirah 支持编译时宏。使用它们,您可以定义在编译时运行的修改语法树的函数。这使您可以将 Java 中常见的一些模式简化为更像 Ruby 中的模式。
例如,
times
是作为宏实现的——尽管它目前是用 Ruby 而不是 Mirah 编写的。您可以像这样使用它
在 Java 中打印出数字 0-4
,它看起来像这样
您当然可以使用
macro def
宏来定义您自己的宏。例如,假设我想使用常见的 logger4j 模式在构造调试字符串之前检查是否启用了调试。使用宏,我可以使检查隐式执行如下操作:我可以这样调用
发生的事情是我正在使用
quote do ... end
创建一个语法树并使用 Mirah 宏引用块中的 ``s 将"something low level is getting on: " + gimme_all_the_bytes_as_a_string
表达式放入其中,取消引用其中的语法树节点。目前关于 Mirah 工作原理的资源并不多,但您可以查看 示例代码 在 Github 上。如果您还有其他问题,请随时向邮件列表发送电子邮件。
Mirah supports compile time macros. With them, you can define functions that are run at compile time that modify the syntax tree. This allows you to simplify some of the common patterns you see in Java into ones more like those found in Ruby.
For example,
times
is implemented as a macro--though it's currently written in Ruby, not Mirah.You can use it like this
to print out the numbers 0-4
in Java it would look something like
You can of course define your own macros by using the
macro def
macro. For example, say I want to use the common logger4j pattern of checking whether debug is enabled before constructing the debug string. With a macro, I could make the check implicit doing something like this:which I could call like this
what's going on there is I'm creating a piece of syntax tree with the
quote do ... end
and dropping the"something low level is going on: " + gimme_all_the_bytes_as_a_string
expression into it using the ``s which in Mirah macro quote blocks, unquote the syntax tree node within them.There currently aren't too many resources about how Mirah works, but you can look at the example code on Github. If you have more questions feel free to send an email to the mailing list.