gpt4 book ai didi

macros - Clojure 如何在源文件中扩展宏与 Repl

转载 作者:行者123 更新时间:2023-12-04 18:44:58 25 4
gpt4 key购买 nike

我在 Clojure 中学习宏并且有一个关于宏扩展的问题。在 repl 中,当我这样做时:

user=> (defmacro unless [pred a b] `(if (not ~pred) ~a ~b))
#'user/unless
user=> (macroexpand-1 '(unless (> 5 3) :foo :bar))
(if (clojure.core/not (> 5 3)) :foo :bar)

但是当我在 clj 文件中做同样的事情时:
(ns scratch-pad.core
(:gen-class))

(defmacro unless [pred a b]
`(if (not ~pred) ~a ~b))

(defn -main [& args]
(prn
(macroexpand-1 '(unless (> 5 3) :foo :bar))))

并运行代码,我得到这个:
$ lein run
(unless (> 5 3) :foo :bar)

如何让代码打印与 repl 相同的代码?

最佳答案

这是怎么回事

这是因为当前命名空间的概念在 Clojure 中是如何工作的。 macroexpand-1在当前命名空间中扩展其参数。

在 REPL 中,这将是 user ;您正在 user 中定义宏命名空间,然后调用 macroexpand-1在那个命名空间中,一切都很好。

:gen-class'd命名空间,或者实际上是任何其他命名空间,编译时当前命名空间就是该命名空间本身。但是,当您稍后调用在此命名空间中定义的代码时,当时的命名空间将是当时适合的任何命名空间。当它被编译时,这可能是其他一些命名空间。

最后,在您的应用程序运行时,默认的当前命名空间是 user .

要看到这一点,您可以将宏移动到一个单独的命名空间,同时定义一个函数 use-the-macro并在顶层调用此函数; :gen-class 'd 命名空间然后需要要求或使用宏的命名空间。然后lein run将打印您期望的内容一次(在宏的命名空间的编译时)和两次未扩展的形式(当宏的命名空间是 require d 主命名空间,然后当 -main 调用 use-the-macro )。

解决方案

Clojure REPL 使用 binding 控制当前命名空间;你也可以做到的:

(binding [*ns* (the-ns 'scratchpad.core)]
(prn (macroexpand-1 ...)))

您也可以在 -main 中使用语法引用而不是引用:
(defn -main [& args]
(prn (macroexpand-1 `...)))
^- changed this

当然如果不是 unless 的符号如果涉及到,您必须决定它们是否应该在输出中符合命名空间,并可能以 ~' 为前缀。 .这就是重点——syntax-quote 适用于生成大部分“与命名空间无关”的代码(除了方便的语法之外,这使得它非常适合编写宏)。

另一个可能的“修复”(在 Clojure 1.5.1 上测试)是添加 in-ns调用 -main :
(defn -main [& args]
(in-ns 'scratchpad.core)
(prn (macroexpand-1 '...)))
^- no change here this time

binding 一样,通过这种方式,您实际上是在原始 namespace 中扩展了原始表单。

关于macros - Clojure 如何在源文件中扩展宏与 Repl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16512291/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com