Thanks! Seems I forgot java.lang.reflect exists when I wrote that.

On Wed, Jun 17, 2009 at 4:47 PM, Michael Reid <kid.me...@gmail.com> wrote:

>
> On Mon, Jun 15, 2009 at 9:13 AM, Parth<parth.malwan...@gmail.com> wrote:
> >
> >
> >
> > 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
> >
>
> Not sure if this can be fit in with what you're trying to do, but you
> can accomplish this with a macro:
>
> user=> (defmacro resolve-sym-str [s] (let [[ns sym] (.split s "/")]
> (symbol ns sym)))
> #'user/resolve-sym-str
> user=> (resolve-sym-str "Math/PI")
> 3.141592653589793
> user=>
>
> However, looking at this further, it seems that the first part of what
> you have tried works:
>
> user=> (import '(java.awt GridBagConstraints))
> nil
> user=> (resolve 'GridBagConstraints)
> java.awt.GridBagConstraints
>
> Now you can use Java's reflection API to grab the field you want:
>
> user=> (-> (resolve 'GridBagConstraints) (.getDeclaredField
> "REMAINDER") (.get nil))
> 0
>
> So it would appear this is possible without a macro, which probably
> makes it easier to use.
>
> /mike
>
> >
>

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