考虑这个伪代码:
(defrc name
"string"
[a :A]
[:div a])
如果defrc是一个宏,那将扩展到以下
(let [a (rum/react (atom :A))]
(rum/defc name < rum/reactive []
[:div a]))
朗姆酒/ defc本身就是一个宏。我想出了下面的代码:
(defmacro defrc
[name subj bindings & body]
(let [map-bindings# (apply array-map bindings)
keys# (keys map-bindings#)
vals# (vals map-bindings#)
atomised-vals# (atom-map vals#)]
`(let ~(vec (interleave keys# (map (fn [v] (list 'rum/react v)) (vals atomised-vals#))))
(rum/defc ~name < rum/reactive [] ~@body))))
哪个几乎有效:
(macroexpand-all '(defrc aname
#_=> "string"
#_=> [a :A]
#_=> [:div a]))
(let* [a (rum/react #object[clojure.lang.Atom 0x727ed2e6 {:status :ready, :val nil}])] (rum/defc aname clojure.core/< rum/reactive [] [:div a]))
但是在使用时会导致语法错误:
ERROR: Syntax error at (clojure.core/< rum.core/reactive [] [:div a])
这是因为内部宏没有被扩展吗?