Yes it works !  :

1:172 user=> (doto (java.awt.Point.) (-> .x (set! 2)))
#<Point java.awt.Point[x=2,y=0]>

I hadn't thought about this possible combination, thanks Meikel.

So now, is mset! worth the trouble ?

Let's compare them again:

(doto (SomeBeanCtor.)
 (-> .field1 (set! expr1))
 (-> .field2 (set! expr2))
 (-> .field4 (set! exprN)))

(mset! (SomeBeanCtor.)
  field1 expr1
  field2 expr2
  field4 expr4)

So is there still some value to mset! in terms of DRY and also readability ?
Maybe, maybe not. It is more succinct, but it looses the ability to mix
method calls and fix assignements.

Anyway, I've changed mset! implementation so that it returns the java
instance instead of the last expression:

(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 instance.
   Example: (mset! (SomeBean.) field1 val1 field2 val2) => val2"
  [inst-expr & field-sym-exprs]
  (let [to-sym (gensym)
        one-set!-fn (fn [[f e]] (list 'set! (list '. to-sym f) e))]
    (concat (list 'let [to-sym inst-expr])
            (map one-set!-fn (partition 2 field-sym-exprs))
            (list to-sym))))


But maybe a more constructive thing could be to extend the current behaviour
of set! :

(set! (. instance-expr instanceFieldName-symbol) expr)
(set! instance-expr instanceFieldName-symbol expr &
instanceFieldName-symbol-exprs)
(set! (. Classname-symbol staticFieldName-symbol) expr)
(set! Classname-symbol staticFieldName-symbol expr &
staticFieldName-symbol-exprs)

So that composition could be total :

(doto (SomeBeanCtor.)
  (set! field1 expr1 field2 expr2 field4 expr4)
  (.method5 arg1 arg2))

?

2009/7/23 Meikel Brandmeyer <m...@kotka.de>

> Hi,
>
> Am 23.07.2009 um 18:24 schrieb Laurent PETIT:
>
>  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 ?
>>
>
> How about that? (untested)
>
> (doto (SomeBeanCtor.)
>  (-> .field1 (set! expr1))
>  (-> .field2 (set! expr2))
>  ...
>  (-> fieldN (set! exprN)))
>
> Sincerely
> Meikel
>
>

--~--~---------~--~----~------------~-------~--~----~
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