How define variables in a macro, so that they available for the caller of the macro
I want to have a macro dbtest that can be used like this:
(dbtest
(prn test-object1)
(prn test-object2))
test-object1 and test-object2 should be variables which are defined by dbtest (because I need them everytime I use the dbtest macro). "prn" is just an example; I want to use arbitrary code inside the 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 insert something into the DB and returns a the corresponding data structure.
But it 开发者_JAVA百科doesn't work: I get a "no such var" error
It 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 isn't with the SQL-related calls?
精彩评论