Hello everybody,
I wanted to define a bunch of types which were mutable to be used in
defining a cyclic data structure. So, I wrote the following macro .. .
(defmacro defmutabletype [type-name members]
(let [proto-name (symbol (str "I" (name type-name)))
member-setter-names (map #(symbol (str (name %) "!")) members)
member-setter-prototypes (map (fn [setter-name] `(~setter-name
[this# newval#])) member-setter-names)
member-setter-fns (map (fn [setter-name member-name] `(~setter-name
[this# newval#] (set! ~member-name newval#))) member-setter-names members)
member-getter-prototypes (map (fn [member-name] `(~member-name
[this#])) members)
member-getter-fns (map (fn [member-name] `(~member-name [this#]
~member-name)) members)
annotated-members (vec (map (fn [name] (with-meta name (assoc (meta
name) :volatile-mutable true))) members))]
`(do
(defprotocol ~proto-name
~@member-getter-prototypes
~@member-setter-prototypes)
(deftype ~type-name ~annotated-members
~proto-name
~@member-getter-fns
~@member-setter-fns))))
(defmutabletype point [x y])
is equivalent to
(do
(defprotocol Ipoint
(x [this])
(x! [this v])
(y [this])
(y! [this v])
(deftype point [^{:volatile-mutable true} x ^{:volatile-mutable true} y]
Ipoint
(x [this] x)
(x! [this v] (set! x v))
(y [this] y)
(y! [this v] (set! y v))))
although not idiomatic clojure .. thought some of you may find it useful.
Sunil.
--
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