我正在尝试使用此技术将值绑定到宏中传入的符号。
(defmacro t2macro [all-bindings body] (reduce (fn [acc [v binding]]
`(let [~v ~binding] ~acc))
`(do ~@body) all-bindings))
#'plans-client.mock/t2macro
plans-client.mock> (t2macro [[a 1] [b 2]] (print (+ a b)))
3
此简化版本可以正常工作。现在,当我尝试在宏列表的上下文中使用相同的技术时,
(defmacro with-mocked-grpc
[mocked-responses & body]
(let [mocks (for [[mock-name [status response]] (partition-all 2 mocked-responses)]
(let [conformed-resp
(specs-util/conform-or-throw ::response response)
generated-resp (check-response conformed-resp mock-name)
bindings (check-bindings generated-resp)]
(with-meta [mock-name [status generated-resp]]
(when bindings {:bindings bindings}))))
all-bindings (map (fn [m] (:bindings (meta m))) mocks)]
(reduce (fn [acc [v binding]]
`(let [~v ~binding] ~acc))
`(do ~@body) all-bindings)))
我这样称呼
(with-mocked-grpc [plans/usage-summary [:ok ^{:sized 100 :binding a} _]] 1)
我得到这个例外
CompilerException java.lang.RuntimeException: Unable to resolve symbol: a in this context, compiling:(*cider-repl plans-service/client:localhost:45691(clj)*:204:20)
check-response为该宏的使用者构建生成的值以供在测试中使用,check-binding查找用户要求绑定到变量的生成的值。
macroexpand
返回
(let [a {:generated :value}] 1)
答案 0 :(得分:1)
宏扩展后元数据仍然可以解析,因此将let行更改为
(let [~(with-meta v nil) ~(with-meta binding nil)] ~acc)
解决了。