我想为调试目的做这样的事情:
(intern 'clojure.core 'doc clojure.repl/doc)
但它不允许我因为编译器说 - 不能取宏的值。
还有另一种方法吗?
最佳答案
宏是存储在元数据映射中带有 :macro true
的 Var 中的函数。这样你就可以
deref
的 Var 获取宏函数本身:@#'doc
intern
通过将适当的元数据附加到名称符号来将函数安装为宏(请参阅 (doc intern)
promise 以这种方式提供的任何元数据传输到 Var):(intern 'clojure.core
(with-meta 'doc {:macro true})
@#'clojure.repl/doc)
也可以使用阅读器元数据,只需记住将其放在“引用内”:
;; attaches metadata to the symbol, which is what we want:
' ^:macro doc
;; attaches metadata to the list structure (quote doc):
^:macro 'doc
^:macro
是 ^{:macro true}
的简写。
关于clojure - 如何在 clojure 中实习宏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20831029/