Skip to content

Instantly share code, notes, and snippets.

@amalloy
Created October 2, 2010 22:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amalloy/608075 to your computer and use it in GitHub Desktop.
Save amalloy/608075 to your computer and use it in GitHub Desktop.
(defmacro defrecord-with-ctor
"Declare a record with the given fields, as well as a
make-x (lower-case) taking a map instead of ordered fields"
[name field-vec]
(let [ctor-name (->> name str .toLowerCase (str "make-") symbol)
nil-args (repeat (count field-vec) nil)]
`(do
(defrecord ~name ~field-vec)
(let [template# (new ~name ~@nil-args)]
(defn ~ctor-name [arg-map#]
(into template# arg-map#))))))
(defrecord-with-ctor Card [suit rank owner])
;; expands to:
(do
(defrecord Card [suit rank owner])
(let [template (new Card nil nil nil)]
(defn make-card [arg-map]
(into template arg-map))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment