Created
November 9, 2011 20:15
-
-
Save gfredericks/1352844 to your computer and use it in GitHub Desktop.
def bean impl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defmacro defbean | |
[class-name | |
field-names | |
& interface-specs] | |
(let [sym-base (str (gensym)), | |
prefix-sym #(->> % name (str sym-base) symbol), | |
setter-name | |
(fn [field-name] | |
(->> field-name name inf/capitalize (str sym-base "set") symbol)), | |
setters | |
(for [field-name field-names] | |
`(defn ~(setter-name field-name) | |
[this# v#] | |
(swap! | |
(. this# ~'state) | |
assoc | |
'~field-name | |
v#))), | |
interface-methods | |
(for [meth interface-specs, :when (sequential? meth)] | |
(let [[meth-name arg-list & body] meth] | |
`(defn ~(prefix-sym meth-name) | |
~arg-list | |
; Create locals for all the field-names | |
(let [{:syms [~@field-names]} (. ~(first arg-list) ~'state)] | |
~@body))))] | |
`(do | |
~@setters | |
~@interface-methods | |
(defn ~(prefix-sym 'init) | |
[] | |
[[] (atom {})]) | |
(gen-class | |
:name ~(str (name (ns-name *ns*)) "." class-name), | |
:prefix ~sym-base, | |
:state ~'state, | |
:init ~'init, | |
:implements [~@(filter symbol? interface-specs)], | |
:methods | |
[~@(for [field-name field-names] | |
[(->> field-name name inf/capitalize (str "set") symbol) | |
[Object] | |
Object])])))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment