Hi,
On Aug 3, 11:43 pm, samppi <
rbysam...@gmail.com> wrote:
> (def my-map {:a 2})
> (defn process-a-map [arg] ...)
> ; Turns maps into corresponding vectors.
> ; (process-a-map my-map) returns ['*foo* :blah].
>
> (my-macro a-map ; Last statement
> (do-something))
The answer which should leave the least grey hair is:
this is not possible. While it is true, that you can use
`eval` to get the map as the others pointed out, you
don't want to do that. Suppose you had my-macro with
`eval`.
(let [my-map {:a 2}]
(my-macro my-map (do-something)))
(defn my-fancy-fn
[the-map-as-arg]
(my-macro the-map-as-arg (do-something)))
Neither of these two forms work and this is a serious
limitation because you are tied to global variables....
> (binding [*foo* :blah] (do-something))
For this special case however you can solve the issue
as follows:
(defn my-macro*
[the-map thunk]
(clojure.lang.Var/pushThreadBindings the-map)
(try
(thunk)
(finally
(clojure.lang.Var/popThreadBindings))))
(defmacro my-macro
[the-map & body]
`(my-macro* ~the-map (fn [] ~@body)))
Hope this helps.
Sincerely
Meikel