Not sure why you need this, but you can create code that parses an arbitrary object using the following cheat:
(def objs (atom [])) (defn make-code-that-evals-to [x] (let [ nobjs (swap! objs #(conj % x)) i (dec (count nobjs))] `(nth ~i @objs)))
Then you can:
> (eval (make-code-that-evals-to *out*)) #<PrintWriter java.io.PrintWriter@14aed2c >
This is just a proof of concept, and it leaks through the generated objects - you can create code that removes the link to eval, but then you can evaluate it only once.
Edit : A leak can be prevented by the following (evil!) Hack:
The above code bypasses the eval compiler by storing the reference to the object outside while creating the code. It can be put off. The object reference can be stored in the generated code when the compiler bypasses the macro. Storing the link in code means the garbage collector is working fine.
A key is a macro that wraps an object. It does what the original decision made (i.e., it stored the object from the outside to bypass the compiler), but immediately before compilation. The generated expression retrieves the external link and then removes it to prevent leaks.
Note: This is evil . Macro expansion time is the least desirable place for global side effects, and this is exactly what this decision does.
Now for the code:
(def objs (atom {}))
Here, where objects are temporarily stored, using unique keys (dictionary).
(defmacro objwrap [x sym] (do (swap! objs
This is an evil macro that resides in the generated code, storing an object reference in x
and a unique key in sym
. Before compilation, it stores the object in an external dictionary under the sym
key and generates code that extracts it, removes the external link, and returns the restored object.
(defn make-code-that-evals-to [x] (let [s 17] ; please replace 17 with a generated unique key. I was lazy here. `(objwrap ~x ~s)))
Nothing unusual, just wrap the object in an evil macro along with a unique key.
Of course, if you only expand the macro without evaluating its result, you will still get a leak.