I've been trying to do some generic work on serializing clojure
objects, and am much impressed with how easy and functional it is, as
well as how easy it is to extend this to other objects (eg Java
classes).

However, I seem to come a little unstuck when it comes to functions /
closures. I realise capturing the state of the environment is going to
be tricky at best, but at the moment I'm trying to extend the basic
defn macro to capture the code used to define the function.
I then tried using print-dup to print the source string from the meta
data, but this is where things stop working.

As far as I can see from the clojure/core-print.clj file, doing a
(binding [*print-dup* true] (pr-str function)) should print out the
meta-data of the function as well, but this does not seem to be
happening. I want this to happen, as I need access to the metadata to
dump the code used instead of the default #=(user
$eval__17$function__19. ) string.

Essentially my new version of defn is a basic wrapper around defn:
(defmacro mkfn [nm args func]
  (list 'defn nm {:source (str "(mkfn " nm " " args " " func ")")}
args func))

so when you do (mkfn test1 [a b] (+ a b)) you get out a function test1
with a metadata :source tag (meta (var test1))
{:ns #<Namespace user>, :name test1, :file "NO_SOURCE_PATH", :line
439, :arglists ([a b]), :tag "sometag", :source "(mkfn test1 [a b] (+
a b))"}

I tried to override the print-dup for clojure.lang.Fn to check for
the :source meta-data tag, but I keep getting NullPointerExceptions.
Anyone got any ideas? Am I missing something obvious?

(defmethod print-dup clojure.lang.Fn [o w]
  (if (meta o) (.write w (:source (meta o)))
    (print-ctor o (fn [o w]) w)))


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