Re: Macro closing over atom gives "Can't embed object in code" error.
I think I understand; the unquote operator is not providing a reference to the underlying object itself, but is rather a procedure that translates the object into a code representation. Thank-you for the explanation. On Sep 19, 6:53 am, Stuart Sierra wrote: > A macro is a function which returns a data structure representing code > to be compiled. All literals (String, Integer, symbol, etc.) and > basic data structures (lists, maps, etc.) can be compiled, as can > functions. > > Most other non-literal objects *cannot* be compiled. > > What you probably want is not a macro but a closure: > > (defn hidden-atom [] > (let [a (atom :hello)] > (fn [] (deref a > > (def x (hidden-atom)) > > (x) > ;;=> :hello > > -S > > On Sep 19, 12:32 am, Nathan Sorenson wrote: > > > I am playing around with a macro to define accessor functions for a > > closed-over atom. Here is a simplified example: > > > (defmacro hidden-atom [] > > (let [a (atom :hello)] > > `(defn get-value [] (deref ~a > > > When I evaluate this macro, I get the error: "Can't embed object in > > code, maybe print-dup not defined: clojure.lang.a...@1a7693a" > > > I imagined this should work, as the above macro doesn't seem too > > different from the following macro, which does work: > > > (defmacro hidden-function[] > > (let [a (fn [] :hello)] > > `(defn get-value [] (~a > > > When using macroexpand-1, both macros return nearly identical forms: > > > gdsl.proc> (macroexpand '(hidden-atom)) > > (def gdsl.proc/get-value (.withMeta (clojure.core/fn gdsl.proc/get- > > value ([] (clojure.core/deref #))) (.meta (var > > gdsl.proc/get-value > > > gdsl.proc> (macroexpand '(hidden-function)) > > (def gdsl.proc/get-value (.withMeta (clojure.core/fn gdsl.proc/get- > > value ([] (# > $a__15...@bde2da>))) (.meta (var gdsl.proc/get-value > > > I'm afraid I don't know enough about macro expansion to understand > > what is wrong here. -- 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
Re: Macro closing over atom gives "Can't embed object in code" error.
A macro is a function which returns a data structure representing code to be compiled. All literals (String, Integer, symbol, etc.) and basic data structures (lists, maps, etc.) can be compiled, as can functions. Most other non-literal objects *cannot* be compiled. What you probably want is not a macro but a closure: (defn hidden-atom [] (let [a (atom :hello)] (fn [] (deref a (def x (hidden-atom)) (x) ;;=> :hello -S On Sep 19, 12:32 am, Nathan Sorenson wrote: > I am playing around with a macro to define accessor functions for a > closed-over atom. Here is a simplified example: > > (defmacro hidden-atom [] > (let [a (atom :hello)] > `(defn get-value [] (deref ~a > > When I evaluate this macro, I get the error: "Can't embed object in > code, maybe print-dup not defined: clojure.lang.a...@1a7693a" > > I imagined this should work, as the above macro doesn't seem too > different from the following macro, which does work: > > (defmacro hidden-function[] > (let [a (fn [] :hello)] > `(defn get-value [] (~a > > When using macroexpand-1, both macros return nearly identical forms: > > gdsl.proc> (macroexpand '(hidden-atom)) > (def gdsl.proc/get-value (.withMeta (clojure.core/fn gdsl.proc/get- > value ([] (clojure.core/deref #))) (.meta (var > gdsl.proc/get-value > > gdsl.proc> (macroexpand '(hidden-function)) > (def gdsl.proc/get-value (.withMeta (clojure.core/fn gdsl.proc/get- > value ([] (# $a__15...@bde2da>))) (.meta (var gdsl.proc/get-value > > I'm afraid I don't know enough about macro expansion to understand > what is wrong here. -- 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
Macro closing over atom gives "Can't embed object in code" error.
I am playing around with a macro to define accessor functions for a closed-over atom. Here is a simplified example: (defmacro hidden-atom [] (let [a (atom :hello)] `(defn get-value [] (deref ~a When I evaluate this macro, I get the error: "Can't embed object in code, maybe print-dup not defined: clojure.lang.a...@1a7693a" I imagined this should work, as the above macro doesn't seem too different from the following macro, which does work: (defmacro hidden-function[] (let [a (fn [] :hello)] `(defn get-value [] (~a When using macroexpand-1, both macros return nearly identical forms: gdsl.proc> (macroexpand '(hidden-atom)) (def gdsl.proc/get-value (.withMeta (clojure.core/fn gdsl.proc/get- value ([] (clojure.core/deref #))) (.meta (var gdsl.proc/get-value gdsl.proc> (macroexpand '(hidden-function)) (def gdsl.proc/get-value (.withMeta (clojure.core/fn gdsl.proc/get- value ([] (#))) (.meta (var gdsl.proc/get-value I'm afraid I don't know enough about macro expansion to understand what is wrong here. -- 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