OK, I want to write a Clojure macro that defines a struct-map and let caller to specify types for each field.
The signature will look like this:
(defmodel category :id Integer :name String)
What this does is it creates a struct-map
called category, and also create a binding *category-meta*
, which is a map {:id Integer :name String}
Here's my macro to achieve this:
(defmacro defmodel [name & field-spec]
`(let [fields# (take-nth 2 ~@field-spec)]
(defstruct ~name fields#)
(def *~name-meta* (reduce #(assoc %1 (first %2) (last %2))) (partition 2 ~@field-spec))))
However, the problem is, I cannot define a binding whose name is composed of another name. Basically, (def *~name-meta* ...)
doesn't work.
How am I able to achieve this?
Thanks.