怎么可能在clojure中实习宏?

zca*_*ate 3 clojure

我想做这样的事情用于调试目的:

(intern 'clojure.core 'doc clojure.repl/doc)
Run Code Online (Sandbox Code Playgroud)

但它并没有让我,因为编译器说 - 不能接受宏的价值.

有另一种方式吗?

Mic*_*zyk 11

宏是:macro true在其元数据映射中存储在Var中的函数.所以你可以

  1. 通过derefVar 获取宏函数本身:

    @#'doc
    
    Run Code Online (Sandbox Code Playgroud)
  2. 用于intern通过将适当的元数据附加到名称符号来将函数安装为宏(请参阅(doc intern)将以这种方式提供的任何元数据转移到Var的承诺):

    (intern 'clojure.core
            (with-meta 'doc {:macro true})
            @#'clojure.repl/doc)
    
    Run Code Online (Sandbox Code Playgroud)

使用读者元数据也是可能的,只需记住将其"置于引号内":

;; attaches metadata to the symbol, which is what we want:
' ^:macro doc

;; attaches metadata to the list structure (quote doc):
^:macro 'doc
Run Code Online (Sandbox Code Playgroud)

^:macro是简写^{:macro true}.