Clojure weird macros when starting cans

The following is a simple Clojure sample application created with 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))

and in project.cljme

:profiles {:uberjar {:aot [mw.core]}}
:main mw.core

When executed in REPL, the score @fsreturns {:macro-f somevalue}. But, managing uberjar, we get {}. If I changed the definition opto defninstead defmacro, it fsagain has the correct content when starting from uberjar. Why is this?

I vaguely understand that this has something to do with AOT compilation and the fact that macro decomposition occurs before the compilation phase, but obviously my understanding of these things is missing.

, mixfix, mixfix . , .

.

!

+4
2

AOT, , - . lein repl ( lein run) uberjar .

lein repl , REPL , mw.core, project.clj, . , , , . ( REPL-), REPL. lein run - , -main .

lein uberjar - , . , clj, ( SO ). , , , , , , , , , . , uberjar java -jar, , ( (op) "" op, ). , .

, .

, , AOT , , ( , , SO , ). :

project.clj:

; ...
:profiles {:uberjar {:aot [mw.main]}}) ; note, no `mw.core` here
; ...

main.clj:

(ns mw.main
  (:gen-class))

(defn get-fs []
  (require 'mw.core)
  @(resolve 'mw.core/fs))

(defn -main [& args]
  (println @(get-fs)))

core.clj:

(ns mw.core
  (:gen-class))

(def fs (atom {}))

(defmacro op []
  (swap! fs assoc :macro-f "somevalue"))

(op)

, , . , .

+2

, . backquote:

(defmacro op []
  `(swap! fs assoc :macro-f "somevalue"))
; ^ syntax-quote ("backquote")

, , clojure .

, , fs, .

, (op) ( ). , REPL, clojure ( . Timur).

+6

Source: https://habr.com/ru/post/1612260/


All Articles