(defrecord Person [name])

(defmulti read-tag (fn [name & args] (keyword "myns" name)))
(defmethod read-tag :myns/person
  [tag name]
  (Person. name))

(read-tag "person" "david")
=> #:myns.Person{:name "david"}

But I agree with everyone else, there's no need to use records unless
you're addicted to objects and classes. Just build a map {:tag-name
"person" :name "david"}, or {:tag-name "person" :attrs {:name
"david"} :contents [...some nested stuff...]}.

On Feb 3, 7:11 pm, Aaron Cohen <aa...@assonance.org> wrote:
> On Thu, Feb 3, 2011 at 10:04 PM, Quzanti <quza...@googlemail.com> wrote:
>
> >> I see no reason for the ctor to be defined as a string as you've done with
> >> "Person.".
>
> > The reason is that I am reading in XML and mapping a tag name to the
> > record class.
>
> It's possible to do this using reflection, but I don't recommend it.
>
> I think you're probably better off making a map of your tag names to
> factory functions, then just applying the factory function to your
> parameters.
>
> (def record-factories {"Person #(Person. %1 %2)})
>
> user=>(apply (record-factories "Person") ["Aaron" 31])
> #:user.Person{:name "Aaron", :age 31}

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