Skip to content

Instantly share code, notes, and snippets.

@polypus74
Created July 8, 2010 00:13
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 polypus74/467471 to your computer and use it in GitHub Desktop.
Save polypus74/467471 to your computer and use it in GitHub Desktop.
(defn dynamic-record [base-name field-names & init-values]
(let [klass (eval `(defrecord ~(gensym base-name) ~(vec field-names)))
ctor (first (.getConstructors klass))
vals (take (count field-names)
(concat init-values (repeat nil)))]
(.newInstance ctor (into-array Object vals))))
(dynamic-record "MyRecord" '(foo bar) :foo)
;; -> #:user.MyRecord14544{:foo :foo, :bar nil}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment