Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clojure defrecord named parameters?

Tags:

clojure

Does defrecord support named parameters? i.e. if if I have something like this:

(defrecord Person [name age])

Can I do something like this:

(Person. {:age 99 :name "bob"})
(Person. :age 99 :name "bob")

The only thing I see by googling is stuff like this:

(Person. "bob" 99)

Which seems less clear...

like image 942
Kevin Avatar asked Aug 23 '11 00:08

Kevin


2 Answers

Not built in, but you could use something like:

(defmulti make-instance (fn [class & rest] class))
(defmacro defrecord* [record-name fields]
  `(do
    (defrecord ~record-name ~fields)
    (defmethod make-instance (quote ~record-name) [_# & {:keys ~fields}]
      (new ~record-name ~@fields))))
(defrecord* Person [name age])
(make-instance 'Person :age 99 :name "bob")

Not sure how suitable that would be for what you want.

like image 127
Hugh Avatar answered Sep 28 '22 11:09

Hugh


It looks like this is not yet supported by clojure?

http://david-mcneil.com/post/765563763/enhanced-clojure-records

like image 44
Kevin Avatar answered Sep 28 '22 13:09

Kevin