1

I am attempting to use this technique bind a value to a passed in symbol in a macro.

(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

This simplified version works fine. Now when i try to use the same technique in the context of my macro list this

(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)))

And i call it like this

(with-mocked-grpc [plans/usage-summary [:ok ^{:sized 100 :binding a} _]] 1)

I get this exception

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 builds a generated value for the consumer of this macro to use in a test, check-binding finds generated values the user asked to be bound to a variable.

macroexpand returns

(let [a {:generated :value}] 1) 

1 Answer 1

1

Metadata still gets resolved after macro expansion so changing the let line to

(let [~(with-meta v nil) ~(with-meta binding nil)] ~acc)

Fixed it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.