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 <[email protected]> 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 #<a...@10a5e22: :hello>))) (.meta (var
> gdsl.proc/get-value))))
>
> gdsl.proc> (macroexpand '(hidden-function))
> (def gdsl.proc/get-value (.withMeta (clojure.core/fn gdsl.proc/get-
> value ([] (#<proc$hidden_function$a__15149 gdsl.proc$hidden_function
> $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 [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en