No description, website, or topics provided.
Switch branches/tags
Nothing to show
Clone or download
Pull request Compare This branch is 8 commits ahead, 9 commits behind david-mcneil:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
README
defrecord2.clj
pom.xml
test-defrecord2.clj

README

Enhance clojure records to:
 * print in an eval'able form
 * provide clojure function as constructor
 * accept named parameters (maps) in constructor
 * participate in pre/post walk multi-method

David McNeil
August 2010

MODIFICATIONS

* modified defrecord2 macro so i could include protocols
* got rid of print-method and pprint method
* fixed support for print-dup so it outputs #= and also namespace of constructor
* changed position of constructor name

USAGE

(defrecord2 name [a b] Protocol1 ...)
or
(defrecord2 (name constructor) [a b] Protocol1 ...)
and you thus create the name record by

(constructor {:a 2 :b 3})
or, if you use the first one
(new-name {:a 2 :b 3})

TO PRINT-DUP

(binding [*print-dup* true] (print-str record))

This can be saved to a file, using something like this:

;;used from
;;http://groups.google.com/group/clojure/browse_thread/thread/cb5246d07142a3dc?fwc=2&pli=1
(defn frm-save 
 "Save a clojure form to file." 
  [file form] 
  (with-open [w (java.io.FileWriter. 
                 (if (instance? File file) file (File. file)))] 
    (binding [*out* w *print-dup* true] (prn form)))) 
(defn frm-load 
  "Load a clojure form from file." 
  [file] 
  (with-open [r (java.io.PushbackReader. 
     (java.io.FileReader. (if (instance? File file) file (File. 
file))))] 
     (let [rec (read r)] 
       rec)))

and atoms and refs can be included by defining these functions:

(defmethod print-dup clojure.lang.Atom [o w]
  (.write w "#=(clojure.core/atom ") (print-dup @o w) (.write w ")"))
(defmethod print-dup clojure.lang.Ref [o w]
  (.write w "#=(clojure.core/ref ") (print-dup @o w) (.write w ")"))