On Jun 15, 7:08 am, James Koppel <darmanith...@gmail.com> wrote:
> I am trying to write a function to simplify working with GridBagConstraints
> -- that is, instead of writing
>
> (let [c (GridBagConstraints.)]
>     (set! (.weightx c) 2.0)
>     (set! (.gridwidth c) GridBagConstraints/REMAINDER)
>     (let [button (JButton. "Hello, world!")]
>       (.setConstraints (.getLayout *my-container*) button c)
>       (.add *my-container* button)))
>
> I could simply write
>
> (gridbag-add *my-container*
>                   (JButton. "Hello, world!")
>                   "weightx=2.0;gridwith=GridBagConstraints/REMAINDER")
>
> A simple combination of regexes and read-string would easily allow me to
> extract the symbol 'GridBagConstraints/REMAINDER from the example string,
> but I'm having trouble actually converting it into its value. Using resolve
> simply returns nil, and getting "." to work dynamically seems to be
> fruitless, as even this simple call
>
> (. (resolve 'GridBagConstraints) REMAINDER)
>
> throws an exception.
>
> So, the question is, how do I go dynamically from a string like
> "GridBagConstraints/REMAINDER" to the actual value of the static field?
>
> Of course, eval does the trick, but I'd rather not have to resort to it.

One way to do that would be to use a map:

user=> (def m {"Math/PI" Math/PI "Math/E" Math/E})
#'user/m
user=> (defn foo [n s] [n (get m s :not-found)])
#'user/foo
user=> (foo 10 "Math/PI")
[10 3.141592653589793]
user=>

You could also consider writing a function that takes these
as parameters and returns the updated container. That way
you can avoid the regex.

Regards,
Parth


>
> Sorry if the answer is obvious; I'm a Clojure-newbie.
--~--~---------~--~----~------------~-------~--~----~
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