下面是一个简单的 Clojure 应用程序示例,使用以下命令创建lein new mw
:
(ns mw.core
(:gen-class))
(def fs (atom {}))
(defmacro op []
(swap! fs assoc :macro-f "somevalue"))
(op)
(defn -main [& args]
(println @fs))
在project.clj
我有
:profiles {:uberjar {:aot [mw.core]}}
:main mw.core
在 REPL 中运行时,评估@fs
返回{:macro-f somevalue}
. 但是,运行 uberjar 会产生{}
. 如果我将op
定义更改为defn
而不是defmacro
,则fs
在从 uberjar 运行时再次具有正确的内容。这是为什么?
我隐约意识到这与AOT编译以及宏扩展发生在编译阶段之前的事实有关,但显然我对这些事情的理解不足。
我在尝试部署一个使用非常好的mixfix库的应用程序时遇到了这个问题,其中 mixfix 运算符是使用全局原子定义的。我花了很长时间才将问题与上述示例隔离开来。
任何帮助将不胜感激。
谢谢!