Skip to content

Instantly share code, notes, and snippets.

@sunilnandihalli
Created March 18, 2011 13:05
Show Gist options
  • Select an option

  • Save sunilnandihalli/876029 to your computer and use it in GitHub Desktop.

Select an option

Save sunilnandihalli/876029 to your computer and use it in GitHub Desktop.
mutable type definition helper .. to automatically create all the setters and getters
(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))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment