On May 22, 12:03 am, Ben Mabey <[email protected]> wrote:
> James Reeves wrote:
> > Hi folks,
>
> > I've been experimenting with the new type system in Clojure 1.2, but
> > I've hit something of a problem. If I define a record:
>
> > user=> (defrecord Foo [])
> > user.Foo
>
> > Then I can coerce a map into a type Foo like so:
>
> > user=> (def m {:x 1, :y 2})
> > #'/user/m
> > user=> (Foo. {} m)
> > #:user.Foo{:x 1, :y 2}
>
> > But what if I define an argument in the record constructor?
>
> > user=> (defrecord Bar [x])
> > user.Bar
>
> > I can't use (Bar. {} m); I'd have to use (Bar. (m :x) {} (dissoc
> > m :x)), but this depends on me knowing in advance that x is an
> > argument in Bar's constructor.
>
> > So is there a programmatic way of telling what keys a record derives
> > from its constructor? Or if there is any other way of coercing a map
> > into an arbitrary record?
>
> > - James
>
> How about merging?
>
> user=> (merge (Bar. {}) m)
> #:user.Bar{:x 1, :y 2}
>
> This approach at least doesn't require that you know in advance the keys
> of Bar.
In general, this does not work. Since
(defrecord Bar [])
(User. {})
raises an exception.
It seems that it would be convenient to have a generic record factory
that receives a map, regardless of the record constructor.
If anyone comes up with one, please let us know ;)
cheers.
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en