Hi,
Am 27.01.2011 um 23:08 schrieb OGINO Masanori:
> It seems ugly for someone using them like function frequently, but
> cool for someone using them like function rarely.
Consider this:
(defn to-factory
[record]
(-> record
name
(.replaceAll "([a-z0-9])([A-Z])" "$1-$2")
.toLowerCase
(->> (str "make-"))
symbol))
(defmacro defrecordx
[name fields & more]
(let [defaults (into {} (map #(if (vector? %) % [% nil]) fields))
fields (vec (map #(if (vector? %) (nth % 0) %) fields))]
`(do
(defrecord ~name ~fields
clojure.lang.IFn
(invoke [this# key#] (get this# key#))
(invoke [this# key# default#] (get this# key# default#))
~@more)
(defn ~(to-factory name)
[& {:keys ~fields :or ~defaults}]
(new ~name ~@fields)))))
Usage example:
(defrecordx Ant [direction [food false]])
(make-ant :direction 0)
vs.
(defstruct ant :direction :food)
(struct-map ant :direction 0 :food false)
I kind of miss the difference in ugliness. If you want default values for your
structs you also need a factory function. So unless you write the factory
function for each struct, you'll also need a defstructx.
records will be the way to go. Even today. And I'm yet to see a reasonable
example, where structs have any advantage over records. records might not be
perfect at the moment, but neither are structs…
Sincerely
Meikel
--
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