Hello,

I want to call set! several times on a same instance, e.g. :
(let [instance (SomeBeanCtor.)]
  (set! (. instance field1) expr1)
  (set! (. instance field2) expr2)
  ...)

Do you know if a macro simplifying this already exists ?

Anyway, I've created one as an exercise, and here it is:

(defmacro mset!
  "Multiple set! calls on the same instance.
   inst-expr is an expression creating the instance on which the set!s will
be
   applied to.
   field-sym-exprs is a succession of field symbols and exprs to be assigned
   to each field.
   Returns the value assigned to the last field.
   Example: (mset! (SomeBean.) field1 val1 field2 val2) => val2"
  [inst-expr & field-sym-exprs]
  (let [field-symbol-exprs (cons field-symbol (cons expr
field-symbol-exprs))
        to-sym (gensym)
        one-set!-fn (fn [[f e]] (list 'clojure.core/set! (list '. to-sym f)
e))]
    (concat (list 'let [to-sym to])
       (map  one-set!-fn
            (partition 2 field-symbol-exprs)))))

I would appreciate if you point me to an existing macro in clojure (or
clojure-contrib) which already does this.

If not, I would also appreciate feedback on the implementation of the macro
: feedback on anything docstring style, choice of binding names, how to do
it more elegantly or readable, etc.

And finally, if you find this would be an interesting addition to
clojure-contrib if it does not already exist ?


Thanks in advance,

-- 
Laurent

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to