Created
October 2, 2010 22:57
-
-
Save amalloy/608075 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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