The problem here is that you are splicing in obj when there is no need to. You actually want your generated code to refer to obj. So just
(defmacro mac1 [& body] `(locking obj ~@body)) is fine. dennis' solution will work too, but it will work almost accidentally? All blank maps evaluate to the same object, so if you do (locking {} (whatever)) it will lock on the same map every time, but if for some reason someone else does the same trick, you'll be competing for the same lock. Better to do what you're doing, but without the unnecessary splice. On Sun, Nov 15, 2015 at 11:17 PM dennis zhuang <killme2...@gmail.com> wrote: > I think the reason is in macroexpand-1 result: > > user=> (macroexpand-1 '(mac1 1)) > (clojure.core/locking #<Object java.lang.Object@7ec0762> 1) > > It's not a valid form to be read and eval by clojure reader,the object > form can't be parsed. > > If define obj to be a map {}, it works fine: > > user=> (def obj {}) > #'user/obj > user=> (mac1 1) > 1 > user=> (macroexpand-1 '(mac1 1)) > (clojure.core/locking {} 1) > > > 2015-11-16 13:55 GMT+08:00 Alice <dofflt...@gmail.com>: > >> user=> (def obj (Object.)) >> #'user/obj >> user=> (defmacro mac1 [& body] `(locking ~obj ~@body)) >> #'user/mac1 >> user=> (mac1 nil) >> CompilerException java.lang.RuntimeException: Can't embed object in code, >> maybe print-dup not defined: java.lang.Object@2a747a37, >> compiling:(NO_SOURCE_PATH:1:1) >> >> >> Why am I getting the error? >> >> -- >> 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 >> --- >> You received this message because you are subscribed to the Google Groups >> "Clojure" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to clojure+unsubscr...@googlegroups.com. >> For more options, visit https://groups.google.com/d/optout. >> > > > > -- > 庄晓丹 > Email: killme2...@gmail.com xzhu...@avos.com > Site: http://fnil.net > Twitter: @killme2008 > > > -- > 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 > --- > You received this message because you are subscribed to the Google Groups > "Clojure" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to clojure+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/d/optout. > -- 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 --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.