Re: Constructing Java Interop calls

2009-10-30 Thread Kevin Downey

eval calls read for somethings.

2009/10/30 Tiago Antão :
>
> On Thu, Oct 29, 2009 at 1:38 PM, Meikel Brandmeyer  wrote:
>>> All good here, but, if I do the eval variation,
>>> user=> (eval (list (symbol ".setFileSelectionMode") jfc 1))
>>
>> Another example which shows that eval is not worth the trouble. It is
>> better to use reflection. You cannot embed the JFileChooser as a
>> object into the code. You have to construct it in your eval. (eval `
>> (let [chooser# (JFileChooser.)] (~(symbol ".setFileSelectionMode")
>> chooser# 1) chosser#)).
>
> Thanks for all the people that helped. But I just wonder, why?
> I mean, why one cannot embed an object in the eval code?
>
> Tiago
>
> >
>



-- 
And what is good, Phaedrus,
And what is not good—
Need we ask anyone to tell us these things?

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



Re: Constructing Java Interop calls

2009-10-30 Thread Tiago Antão

On Thu, Oct 29, 2009 at 1:38 PM, Meikel Brandmeyer  wrote:
>> All good here, but, if I do the eval variation,
>> user=> (eval (list (symbol ".setFileSelectionMode") jfc 1))
>
> Another example which shows that eval is not worth the trouble. It is
> better to use reflection. You cannot embed the JFileChooser as a
> object into the code. You have to construct it in your eval. (eval `
> (let [chooser# (JFileChooser.)] (~(symbol ".setFileSelectionMode")
> chooser# 1) chosser#)).

Thanks for all the people that helped. But I just wonder, why?
I mean, why one cannot embed an object in the eval code?

Tiago

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



Re: Constructing Java Interop calls

2009-10-29 Thread Kevin Downey

user=> ((eval `(fn [x#] (~(symbol ".setFileSelectionMode") x# 1))) jfc)
nil
user=>


On Thu, Oct 29, 2009 at 6:38 AM, Meikel Brandmeyer  wrote:
>
> Hi,
>
> On Oct 29, 2:07 pm, Tiago Antão  wrote:
>
>> The eval form still shows some problems, if I do this preparation:
>>
>> (import javax.swing.JFileChooser)
>> (def jfc (new JFileChooser))
>>
>> And then do:
>>
>> user=> (.setFileSelectionMode jfc 1)
>> nil
>>
>> All good here, but, if I do the eval variation,
>> user=> (eval (list (symbol ".setFileSelectionMode") jfc 1))
>
> Another example which shows that eval is not worth the trouble. It is
> better to use reflection. You cannot embed the JFileChooser as a
> object into the code. You have to construct it in your eval. (eval `
> (let [chooser# (JFileChooser.)] (~(symbol ".setFileSelectionMode")
> chooser# 1) chosser#)).
>
> Hopes this helps.
>
> Sincerely
> Meikel
>
> >
>



-- 
And what is good, Phaedrus,
And what is not good—
Need we ask anyone to tell us these things?

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



Re: Constructing Java Interop calls

2009-10-29 Thread Meikel Brandmeyer

Hi,

On Oct 29, 2:07 pm, Tiago Antão  wrote:

> The eval form still shows some problems, if I do this preparation:
>
> (import javax.swing.JFileChooser)
> (def jfc (new JFileChooser))
>
> And then do:
>
> user=> (.setFileSelectionMode jfc 1)
> nil
>
> All good here, but, if I do the eval variation,
> user=> (eval (list (symbol ".setFileSelectionMode") jfc 1))

Another example which shows that eval is not worth the trouble. It is
better to use reflection. You cannot embed the JFileChooser as a
object into the code. You have to construct it in your eval. (eval `
(let [chooser# (JFileChooser.)] (~(symbol ".setFileSelectionMode")
chooser# 1) chosser#)).

Hopes this helps.

Sincerely
Meikel

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



Re: Constructing Java Interop calls

2009-10-29 Thread Tiago Antão

On Thu, Oct 29, 2009 at 2:15 AM, Alex Osborne  wrote:
>
> Using eval (which will also work for dynamically calling Clojure functions):
>
>   (let [obj "some string"
>         fname ".substring"]
>     (eval (list (symbol fname) obj 2)))

Thanks a lot. I was trying to avoid reflection (ie, looking for a
clojure idiom), especially to get an understanding on what are clojure
limitations.

The eval form still shows some problems, if I do this preparation:

(import javax.swing.JFileChooser)
(def jfc (new JFileChooser))

And then do:

user=> (.setFileSelectionMode jfc 1)
nil

All good here, but, if I do the eval variation,
user=> (eval (list (symbol ".setFileSelectionMode") jfc 1))
I get:
# (println (list (symbol ".setFileSelectionMode") jfc 1))
(.setFileSelectionMode # 1)

So the list for the eval looks good...

Many thanks,
Tiago

-- 
"The hottest places in hell are reserved for those who, in times of
moral crisis, maintain a neutrality." - Dante

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



Re: Constructing Java Interop calls

2009-10-28 Thread John Harrop
On Wed, Oct 28, 2009 at 10:15 PM, Alex Osborne  wrote:

>
> Tiago Antão wrote:
> > Again, the point here is to be able to construct method names (full
> > call signatures, really) on runtime.
> >
> > I am lost. As in newbie clueless :(
>
> As others have suggested you need to use either Java's reflection or
> Clojure's eval.


Not quite -- this works as long as the "Bla" part is a constant:

(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)

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



Re: Constructing Java Interop calls

2009-10-28 Thread Alex Osborne

Tiago Antão wrote:
> Again, the point here is to be able to construct method names (full
> call signatures, really) on runtime.
> 
> I am lost. As in newbie clueless :(

As others have suggested you need to use either Java's reflection or 
Clojure's eval.  Here's some examples:

Using reflection:

   (let [obj "some string"
 method (.getDeclaredMethod (class obj) "substring"
(into-array Class [Integer/TYPE]))]
 (.invoke method obj (to-array [2])))

   => "me string"

If you want to know more about what you can do with reflection, consult: 
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html

Using eval (which will also work for dynamically calling Clojure functions):

   (let [obj "some string"
 fname ".substring"]
 (eval (list (symbol fname) obj 2)))

   => "me string"




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



Re: Constructing Java Interop calls

2009-10-28 Thread Kevin Downey

you can always just construct the call as a string or as a
datastructure and pass it through read/eval

On Wed, Oct 28, 2009 at 2:04 PM, Meikel Brandmeyer  wrote:
> Hi,
>
> Am 28.10.2009 um 20:46 schrieb Tiago Antão:
>
>> But my point is to be able to construct the method name in runtime.
>
> You'll need reflection for that. AFAIU method calls are wired in the
> bytecode and hence the method name must be known at compile time.
>
> Sincerely
> Meikel
>
>



-- 
And what is good, Phaedrus,
And what is not good—
Need we ask anyone to tell us these things?

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



Re: Constructing Java Interop calls

2009-10-28 Thread Meikel Brandmeyer
Hi,

Am 28.10.2009 um 20:46 schrieb Tiago Antão:

> But my point is to be able to construct the method name in runtime.

You'll need reflection for that. AFAIU method calls are wired in the  
bytecode and hence the method name must be known at compile time.

Sincerely
Meikel



smime.p7s
Description: S/MIME cryptographic signature


Re: Constructing Java Interop calls

2009-10-28 Thread Michael Wood

2009/10/28 Tiago Antão :
[...]
> Again, the point here is to be able to construct method names (full
> call signatures, really) on runtime.
>
> I am lost. As in newbie clueless :(

I suspect you want reflection, but I don't know off hand how to do it.

-- 
Michael Wood 

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



Re: Constructing Java Interop calls

2009-10-28 Thread Tiago Antão

On Wed, Oct 28, 2009 at 7:34 PM, Wilson MacGyver  wrote:
>
> Is there a reason you don't want to use doto?
>
> http://clojure.org/java_interop#toc15
>
> ie (doto Bla (.setProperty "x" 1))

I really want to do something different:

(def x (new StringBuffer ""))
(doto x (.setLength 2))

But my point is to be able to construct the method name in runtime.
Using doto, I still have the same problem:

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

If I do:
(def sb (new StringBuffer ""))
(setProperty "Length" sb 2)
sb

I get
"" (It should be "aa").

Again, the point here is to be able to construct method names (full
call signatures, really) on runtime.

I am lost. As in newbie clueless :(

Thanks a lot,
T

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



Re: Constructing Java Interop calls

2009-10-28 Thread John Harrop
2009/10/28 Tiago Antão 

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



Re: Constructing Java Interop calls

2009-10-28 Thread Wilson MacGyver

Is there a reason you don't want to use doto?

http://clojure.org/java_interop#toc15

ie (doto Bla (.setProperty "x" 1))

2009/10/28 Tiago Antão :
>
> 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 I do
>
> (println (setProperty "Bla" x 1))
> I get 1 (the setBla is a void, so I should not get 1).
> bla is never set to 1, by the way :(
>
> Note that I am not trying to sort the particular case of beans. I am
> trying to understand the general mechanism to construct symbols (java
> interop) and execute them with a set of parameters over a java object.
>
> Thanks a lot and sorry for the newbie question,
> T
>
> --
> "The hottest places in hell are reserved for those who, in times of
> moral crisis, maintain a neutrality." - Dante
>
> >
>



-- 
Omnem crede diem tibi diluxisse supremum.

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