How to define variables in a macro so that it is accessible to the calling macro

I want to have a dbtest macro that can be used as follows:

(dbtest (prn test-object1) (prn test-object2)) 

test-object1 and test-object2 should be the variables that dbtest defines (because I need them every time I use dbtest macro). "prn" is just an example; I want to use arbitrary code inside a macro.

I tried this:

 (defmacro dbtest [& body] `(sql/with-connection db (delete-all-tables) (let [~'test-object1 (insert-object "foo")] ~@body ))) 

where insert-object is a function that inserts something into the database and returns the corresponding data structure.

But this does not work: I get the error "no such var"

+4
source share
1 answer

This works for me:

 user=> (defmacro let-test [& body] `(let [~'test-object1 123] ~@body )) #'user/let-test user=> (let-test (+ test-object1 321)) 444 

Are you sure the problem is not with SQL related calls?

+5
source

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


All Articles