> On Jan 17, 2015, at 12:27 PM, Sven Richter <sver...@googlemail.com> wrote: > > I am trying to create record definitions dynamically during runtime. What I > would like to do is something like this: > > (defn mk-rec [record-name namespace arg-list] > (eval '(do (ns namespace) > (defrecord record-name arg-list)))) > > And then call it like this: > (mk-rec "A" "ns" [a b c]) > (->ns.A 1 2 3 ) > > I am trying out different things, but struggle to get it right or find some > tutorial for it. > > So if anyone is willing to share some working code I'd be really happy.
In case it may help I'll paste in below some related code from my Clojush project (https://github.com/lspector/Clojush <https://github.com/lspector/Clojush>). The stuff commented out at the top is how I used to do it with defstruct, which I preferred, but that was a very unpopular perspective. The stuff below is how we do it now with defrecord. Best, -Lee ;(defmacro define-push-state-structure [] ; `(defstruct push-state ~@push-types)) ;(define-push-state-structure) ;(defn make-push-state ; "Returns an empty push state." ; [] ; (struct-map push-state)) ;; record-based states (apparently faster) (defn keyword->symbol [kwd] "Returns the symbol obtained by removing the : from a keyword." (symbol (name kwd))) (defmacro define-push-state-record-type [] `(defrecord ~'PushState [~@(map keyword->symbol push-types)])) (define-push-state-record-type) (let [empty-state (map->PushState {})] (defn make-push-state "Returns an empty push state." [] empty-state)) -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.