If there are a unknown number of layouts you can just define a map from
keywords to layouts: {:x_axis BoxLayout/x_axis ..}

Otherwise using java reflection is another option.

Thanks,
Ambrose

On Thu, Mar 19, 2015 at 4:34 PM, Mark Bastian <markbast...@gmail.com> wrote:

> To provide a little more context, the problem I am trying to solve is
> this: Often in Java I see constructors that have a pattern of (ClassName.
> X) where X is some static integer value. For example, in Swing you build a
> Box like so (Box. BoxLayout/X_AXIS). I want to simplify this by doing
> something along the lines of (make-box :x_axis) by building the symbol and
> passing it into the constructor. Here is a complete example of how I've
> solved the problem using eval:
>
> (import '(javax.swing Box BoxLayout))
>
> (defn make-symbol[sym] (->> x name .toUpperCase (str "BoxLayout/") symbol))
>
> ;This works, but I am eval-ing. Seems like I could use a macro instead.
> (defn make-box[box-type]
>   (Box. (eval (make-symbol box-type))))
>
> ;Works
> (make-box :x_axis)
>
> ;Works
> (map make-box [:x_axis :y_axis])
>
> ;My first attempt using a macro
> (defmacro make-box-macro[box-type]
>   `(Box. (make-symbol ~box-type)))
>
> ;ClassCastException - Symbol cannot be cast to java.lang.Number
> (make-box-macro :x_axis)
>
> ;Hmmm, works, but still using eval. Might as well use the defn.
> (defmacro make-box-macro2[box-type]
>   `(let[x# (make-symbol ~box-type)]
>      (Box. (eval x#))))
>
> (make-box-macro2 :x_axis)
>
> Hopefully one of you can point out some small change I need to make to
> make this work. Any help would be appreciated. I've read brave clojure and
> learnxinyminutes, but I am still missing something.
>
> Thanks,
> Mark
>
> --
> 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
> ---
> You received this message because you are subscribed to the Google Groups
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to clojure+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to