请看以下伪代码:
(defrc name
"string"
[a :A]
[:div a])
defrc将是一个宏,它会扩展为以下内容
(let [a (rum/react (atom :A))]
(rum/defc name < rum/reactive []
[:div a]))
其中rum/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])
这是因为内部宏没有被展开吗?