2017-09-02 87 views
2

我不明白如何獲得完整的宏觀擴展。爲什麼macroexpansion不能擴展Clojure中的嵌套窗體?

有了這個代碼

(when true (when true true)) 

我想獲得完整的宏擴展

(if true (do (if true (do true))) 

但我不能

我明白macroexpansion-1將解決擴展的第一級:

(macroexpand-1 '(when true (when true true))) 

(if true (do (when true true))) 

但是,爲什麼當我再次打電話macroexpand-1(這是應該做的macroexpand):

(macroexpand-1 '(if true (do (when true true)))) 

我得到了完全相同的結果?

(if true (do (when true true))) 

我期待着全面的宏觀擴張。

宏擴展只適用於頂層表單嗎?

我知道在clojure.walk命名空間中的expand-all功能的,所以我想 macroexpand不能在嵌套結構的工作。我對嗎 ?

回答

6

你說得對。

又見https://clojuredocs.org/clojure.core/macroexpand

凡指出:

注既不macroexpand-1或macroexpand擴大宏在子窗體。

事實上macroexpand,所有做遞歸擴展:

> (clojure.walk/macroexpand-all '(when true (when true true))) 
(if true (do (if true (do true)))) 

參見https://clojuredocs.org/clojure.walk/macroexpand-all

它指出:

執行遞歸形式的所有可能的macroexpansions。

你的榜樣

(macroexpand-1 '(if true (do (when true true)))) 

可能混淆你,但它的文檔告訴:

(macroexpand-1表格)如果形式表示宏形式,返回其 擴張,否則返回表單。

因此,'if'不是一個宏,它只是返回,如果沒有進入子表單...