Last active
July 3, 2016 19:26
-
-
Save RutledgePaulV/dfb6b0ce31be3fbeef05e73316abaef7 to your computer and use it in GitHub Desktop.
A macro wrapping def-record that also defines a constructor function with default values.
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 make-model | |
[name args & body] | |
(let [defaults (if (map? (first body)) (first body) {}) | |
constructor-name (str/lower-case (str "make-" name))] | |
`(do (defrecord ~name ~args ~@(if (map? (first body)) (rest body) body)) | |
(defn ~(symbol constructor-name) | |
([] (~(symbol constructor-name) {})) | |
([values#] (~(symbol (str "map->" name)) (merge ~defaults values#))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage like:
Default arguments are optional and so too are any arguments to the constructor.