2009/10/28 Tiago Antão <tiagoan...@gmail.com>

>
> Hi,
>
> Sorry for the newbie question, but I am trying to understand how to
> construct and call java dynamically from clojure.
> As an example, imagine that there is a bean property called "Bla" and
> one wants to set Bla to 1 on object x, which has that property.
> So, the objective would be to construct, the following java equivalent
>
> x.setBla(1);
>
> I defined a macro called setProperty like this:
> (setProperty "Bla" x 1)
>
> And the definition is (very wrong):
> (defmacro setProperty [field obj value]
>  `(let [cct# (symbol (.concat ".set" ~field))]
>    (cct# ~obj ~value)
>  )
> )


If "field" is only ever going to be a literal:

(defmacro setProperty [field obj value]
  (let [cct (symbol (.concat ".sub" field))]
    `(~cct ~obj ~value)))

user=> (setProperty "string" "foobar" 3)
"bar"

Of course in that case it's nicer if you can omit the quotation marks:

(defmacro setProperty [field obj value]
  (let [cct (symbol (.concat ".set" (str field)))]
    `(~cct ~obj ~value)))

user=> (macroexpand-1 '(setProperty Bla x 1))
(.setBla x 1)

But this won't work if the first argument to setProperty is an expression
that evaluates to Bla rather than the literal symbol Bla.

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