On Tue, May 11, 2010 at 6:18 AM, Konrad Hinsen
<konrad.hin...@fastmail.net> wrote:
>> Right now I can imagine that implementation would look like this. Each
>> function has it own raw input form (string which is parsed by clj
>> reader). On serialization, that raw form would be serialized and
>> received on the other unit. With first call, such deserialized
>> function would check if it is compiled, if not then it compiles its
>> raw form then executes it.
>
> That's not sufficient because you have to serialize closures, not plain 
> functions. You need more than just the source code.

Actually in Clojure 1.2 the closure's lexical environment is exposed
to macros, so this is doable. The serializable-fn tool that Kevin
linked to demonstraes a primitive form of this, though I haven't tried
closing over too many different types yet.

(defn- save-env [env form]
  (if env
    `(let ~(vec (apply concat (for [[name local] env]
                                [name (.eval (.init local))])))
       (~...@form))
    form))

(defmacro fn [& sigs]
  `(with-meta (clojure.core/fn ~...@sigs)
     {:type ::serializable-fn
      ::source (quote ~(save-env &env &form))}))

The problem is that the .eval method isn't supported on all expression
types; it needs a more generalized way of getting the value of any
compiler expression. I haven't had a chance to really dig into the
compiler to see if there's a single way to do that, but I wouldn't be
surprised if there were a simpler solution.

-Phil

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