On Sun, Dec 18, 2011 at 2:56 PM, Bill Robertson
<billrobertso...@gmail.com> wrote:
> I'm trying to define records based on meta data read in at runtime,
> and I'm attempting to write a function that extracts name and attrs
> and passes them to defrecord, but I don't understand how to
> dynamically create the symbol for the record name.
>
> e.g.
>
> user=> (defrecord (symbol "foo" "bar") [a b])
> java.lang.ClassCastException: clojure.lang.PersistentList cannot be
> cast to clojure.lang.Symbol (NO_SOURCE_FILE:5)
>
> Attempting to define fields in any way that I can guess at fail.
>
> e.g.
>
> user=> (def fields [(symbol "a")])
> #'user/fields
> user=> (defrecord foo fields)
> IllegalArgumentException Don't know how to create ISeq from:
> clojure.lang.Symbol  clojure.lang.RT.seqFrom (RT.java:487)
> user=> (defrecord foo (var fields))
> user.foo
> user=> (user/map->foo {:a 67})       ;; funny
> #user.foo{:var nil, :fields nil, :a 67}
>
> Short of generating a string and calling eval on it, is this possible?

Sort of. For dynamic defrecording of dynamically-determined names at
runtime, eval is needed, but eval operates on syntax fragments, not
strings, so you don't need to muck about with strings and escaping and
all that crap. Try

(eval `(do (in-ns '~'foo) (defrecord ~(symbol "Bar") [~'a ~'b])))

in the REPL and see what results. Note that it's actually very similar
to macro writing.

-- 
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

Reply via email to