Re: Coercing a map to a record

2010-05-23 Thread Pedro Teixeira
On May 22, 12:03 am, Ben Mabey 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 Fo

Re: Coercing a map to a record

2010-05-22 Thread James Reeves
On 22 May 2010 04:03, Ben Mabey wrote: > 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. No, you still do: user=> (defrecord Foo [x y z]) user.Foo user=> (merge (Foo. {}) m) No match

Re: Coercing a map to a record

2010-05-22 Thread Ben Mabey
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

Re: Coercing a map to a record

2010-05-22 Thread James Reeves
Good point. I keep falling into the trap of treating types like classes. Types don't have inline constructors, because normal functions perform that task well enough. If I want to coerce a map into a type, I really need just a constructor function, rather than the type itself. In this case, someth

Re: Coercing a map to a record

2010-05-21 Thread Meikel Brandmeyer
Hi, user=> (defrecord Bar [x]) user.Bar user=> (into (Bar. nil) {:x 1 :y 2}) #:user.Bar{:x 1, :y 2} One still has to know, that Bar takes an argument, but one could provide and API function which takes care of that. user=> (defrecord Bar [x]) user.Bar user=> (defn empty-bar [] (Bar. nil)) #'user

Coercing a map to a record

2010-05-21 Thread James Reeves
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