On Fri, Mar 11, 2011 at 11:52 PM, Stuart Sierra
<the.stuart.sie...@gmail.com> wrote:
> `eval` invokes the Clojure compiler, which transforms data structures into
> Java bytecode.  The Clojure compiler understands Clojure data structures
> like lists, vectors, and symbols, plus a few Java types like String and
> numbers.  It doesn't know what to do with a java.util.Date.  "Can't embed
> object in code" is the compiler telling you "I don't know what to do with
> this."

Yes, but "create a static final member in the class I'm generating
bytecode for, stuff the object in that static member, and embed a
reference to the static member here" seems like a sensible thing for
it to do.

private static final Date blah = somedate;

...

something.somemethod(blah);

works in javac, but clojure's compiler hates

(eval '(.somemethod something ~somedate))

for whatever reason.

> The #= is a Clojure reader macro that means "evaluate the following code."
> The error message "maybe print-dup not defined" is suggesting that, if you
> really did mean to stick a java.util.Date in your compiled code, you have to
> provide your own implementation of print-dup.  Specifically, you need a
> method of print-dup that takes a java.util.Date and returns a data structure
> representing the Clojure code that *creates* a java.util.Date.

So, there is a workaround of sorts. A general one for serializable
objects could even be constructed: serialize to a byte array, embed
the byte array as a seq in code that turns it back into a byte array
and then deserializes an object from it. Not very efficient though.

A true fix requires changing the compiler to add static final members
to the generated class, as suggested above.

-- 
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

Reply via email to