Alex Baranosky <alexander.barano...@gmail.com> writes: > I've been playing with making a macro to encapsulate Stuart's post, like this: > > (defmacro defrecord-ifn [name & args] > `(defrecord ~name ~...@args > clojure.lang.IFn > (invoke [this key] (get this key)))) > > (defrecord-ifn Foo [a b c]) > > (def foo (Foo. "A" "B" "C")) > > (prn (map foo [:a :c])) => ("A", "C") > > I get the error: > > "No such var: user/this". I guess this is because it is expanding > 'this' to 'user/this'. What is the proper way to get a macro like > this to expand properly?
Use autogensym. Stick a # on the end of bindings you declare inside syntax-quote: this# and key#. (defmacro defrecord-ifn [name & args] `(defrecord ~name ~...@args clojure.lang.IFn (invoke [this# key#] (get this# key#)))) -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en