chris a écrit :
> Hello, I am gearing up to write some swing code, specifically some
> stuff where I want to use the grid bag layout system, and I remember
> something I hated about java:
>
> --c.fill = GridBagConstraints.HORIZONTAL;
> c.weightx = 0.5;
> c.gridx = 2;
> c.gridy = 0;
> --
>
> You repeated c all over the place so you didn't dare name it something
> reasonable.
>
> I noticed in some closure code this looks about the same (actually
> worse):
> (set! (.fill c) GridBagConstraints/HORIZONTAL)
>     (set! (.anchor c) GridBagConstraints/PAGE_END)
>     (set! (.weightx c) 1.0)
>     (set! (.weighty c) 1.0)
>   
Your sets! macro seems a good idea to me.

When an object provides proper accessors, you can write:

(doto c
  (.setFill GridBagConstraints/HORIZONTAL)
  (.setAnchor GridBagConstraints/PAGE_END)
  (.setWeightx 1.0)
  (.setWeighty 1.0))

But in your case, the only way to use doto I can think of is:

(doto c
  (-> .fill (set! GridBagConstraints/HORIZONTAL))
  (-> .anchor (set! GridBagConstraints/PAGE_END))
  (-> .weightx (set! 1.0))
  (-> .weighty (set! 1.0)))

which does not repeat c but is even slightly longer than the naive 
sequence of set!.

I think that you could remove some parens from your macro form to just 
write:

(sets! c 
  fill GridBagConstraints/HORIZONTAL
  weightx 1.0)

(I think it's a more idiomatic form, see cond or bindings: pairs aren't 
paired together.)

(defmacro sets! [vars & rest]
  `(do ~@(map (fn [flds] `(set! (. ~vars ~(first flds)) ~(second flds))) 
(apply array-map rest))))

Christophe

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