How to get a value of a var in Java

2021-06-18 Thread ru
Dear Clojure users and team!
Citation 1 from Clojure documentation:

"Calling Clojure From Java..
IFn plus = Clojure.var("clojure.core", "+"); 
plus.invoke(1, 2);.."

With functions all well but with def..

Citation 2:

"(def *symbol* *doc-string*? *init*?)

Creates and interns or locates a global var 
 with the name of *symbol* and a 
namespace of the value of the current namespace (*ns*). "

I can not figure out how to get the value of the symbol, created with def 
construct in Clojure and, for example, print it with System.out.println in 
Java.

I tried everything, nothing works! :(

Any help would be greatly appreciated.

Sincerely,

  Ru





-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/clojure/fcbd3883-705a-4180-9b99-7ccad64a09afn%40googlegroups.com.


Re: How to get a value of a var in Java

2021-06-18 Thread Alex Ott
does this answer your q:
https://stackoverflow.com/questions/2181774/calling-clojure-from-java/2182608#2182608
?

On Fri, Jun 18, 2021 at 12:53 PM ru  wrote:

> Dear Clojure users and team!
> Citation 1 from Clojure documentation:
>
> "Calling Clojure From Java..
> IFn plus = Clojure.var("clojure.core", "+");
> plus.invoke(1, 2);.."
>
> With functions all well but with def..
>
> Citation 2:
>
> "(def *symbol* *doc-string*? *init*?)
>
> Creates and interns or locates a global var
>  with the name of *symbol* and a
> namespace of the value of the current namespace (*ns*). "
>
> I can not figure out how to get the value of the symbol, created with def
> construct in Clojure and, for example, print it with System.out.println in
> Java.
>
> I tried everything, nothing works! :(
>
> Any help would be greatly appreciated.
>
> Sincerely,
>
>   Ru
>
>
>
>
>
> --
> 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.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/clojure/fcbd3883-705a-4180-9b99-7ccad64a09afn%40googlegroups.com
> 
> .
>


-- 
With best wishes,Alex Ott
http://alexott.net/
Twitter: alexott_en (English), alexott (Russian)

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/clojure/CALV1_%3DKecD-Jtkkz%2BWZ5vn48ZHqq9EuZttZN6gJAWvv1_w8fOg%40mail.gmail.com.


Re: How to get a value of a var in Java

2021-06-18 Thread ru
Thanx Alex!

Alas, with defn it works, with def - not! :(

пятница, 18 июня 2021 г. в 15:31:03 UTC+3, Alex Ott: 

> does this answer your q: 
> https://stackoverflow.com/questions/2181774/calling-clojure-from-java/2182608#2182608
>  
> ?
>
> On Fri, Jun 18, 2021 at 12:53 PM ru  wrote:
>
>> Dear Clojure users and team!
>> Citation 1 from Clojure documentation:
>>
>> "Calling Clojure From Java..
>> IFn plus = Clojure.var("clojure.core", "+"); 
>> plus.invoke(1, 2);.."
>>
>> With functions all well but with def..
>>
>> Citation 2:
>>
>> "(def *symbol* *doc-string*? *init*?)
>>
>> Creates and interns or locates a global var 
>>  with the name of *symbol* and a 
>> namespace of the value of the current namespace (*ns*). "
>>
>> I can not figure out how to get the value of the symbol, created with def 
>> construct in Clojure and, for example, print it with System.out.println in 
>> Java.
>>
>> I tried everything, nothing works! :(
>>
>> Any help would be greatly appreciated.
>>
>> Sincerely,
>>
>>   Ru
>>
>>
>>
>>
>>
>> -- 
>> You received this message because you are subscribed to the Google
>> Groups "Clojure" group.
>> To post to this group, send email to clo...@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+u...@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+u...@googlegroups.com.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/clojure/fcbd3883-705a-4180-9b99-7ccad64a09afn%40googlegroups.com
>>  
>> 
>> .
>>
>
>
> -- 
> With best wishes,Alex Ott
> http://alexott.net/
> Twitter: alexott_en (English), alexott (Russian)
>

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/clojure/ff4cb2f0-b121-4c54-b060-a1e64fbb0e4an%40googlegroups.com.


Re: How to get a value of a var in Java

2021-06-18 Thread Chris Nuernberger
Vars extend IDeref so one way to get the value of a var is to call .deref
on it:

user> (def a)
#'user/a
user> (defn ab [] 1)
#'user/ab
user> (.deref (clojure.lang.RT/var "user" "a"))
2
user> (.deref (clojure.lang.RT/var "user" "ab"))
#function[user/ab]



On Fri, Jun 18, 2021 at 7:12 AM ru  wrote:

> Thanx Alex!
>
> Alas, with defn it works, with def - not! :(
>
> пятница, 18 июня 2021 г. в 15:31:03 UTC+3, Alex Ott:
>
>> does this answer your q:
>> https://stackoverflow.com/questions/2181774/calling-clojure-from-java/2182608#2182608
>> ?
>>
>> On Fri, Jun 18, 2021 at 12:53 PM ru  wrote:
>>
>>> Dear Clojure users and team!
>>> Citation 1 from Clojure documentation:
>>>
>>> "Calling Clojure From Java..
>>> IFn plus = Clojure.var("clojure.core", "+");
>>> plus.invoke(1, 2);.."
>>>
>>> With functions all well but with def..
>>>
>>> Citation 2:
>>>
>>> "(def *symbol* *doc-string*? *init*?)
>>>
>>> Creates and interns or locates a global var
>>>  with the name of *symbol* and a
>>> namespace of the value of the current namespace (*ns*). "
>>>
>>> I can not figure out how to get the value of the symbol, created with
>>> def construct in Clojure and, for example, print it with System.out.println
>>> in Java.
>>>
>>> I tried everything, nothing works! :(
>>>
>>> Any help would be greatly appreciated.
>>>
>>> Sincerely,
>>>
>>>   Ru
>>>
>>>
>>>
>>>
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Clojure" group.
>>> To post to this group, send email to clo...@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+u...@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+u...@googlegroups.com.
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/clojure/fcbd3883-705a-4180-9b99-7ccad64a09afn%40googlegroups.com
>>> 
>>> .
>>>
>>
>>
>> --
>> With best wishes,Alex Ott
>> http://alexott.net/
>> Twitter: alexott_en (English), alexott (Russian)
>>
> --
> 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.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/clojure/ff4cb2f0-b121-4c54-b060-a1e64fbb0e4an%40googlegroups.com
> 
> .
>

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/clojure/CADbpEJumuL42Zqh29LnsDo6BUUQTjT8gbXrsW6%3DAOPRoOBkNNA%40mail.gmail.com.


Re: How to get a value of a var in Java

2021-06-18 Thread ru
Can you please rewrite your example in Java, that I can try it?
By the way, can you give a reference on javadoc in witch IDeref class 
documented? 
Thanks in advance.

пятница, 18 июня 2021 г. в 16:17:36 UTC+3, ch...@techascent.com: 

> Vars extend IDeref so one way to get the value of a var is to call .deref 
> on it:
>
> user> (def a)
> #'user/a
> user> (defn ab [] 1)
> #'user/ab
> user> (.deref (clojure.lang.RT/var "user" "a"))
> 2
> user> (.deref (clojure.lang.RT/var "user" "ab"))
> #function[user/ab]
>
>
>
> On Fri, Jun 18, 2021 at 7:12 AM ru  wrote:
>
>> Thanx Alex!
>>
>> Alas, with defn it works, with def - not! :(
>>
>> пятница, 18 июня 2021 г. в 15:31:03 UTC+3, Alex Ott: 
>>
>>> does this answer your q: 
>>> https://stackoverflow.com/questions/2181774/calling-clojure-from-java/2182608#2182608
>>>  
>>> ?
>>>
>>> On Fri, Jun 18, 2021 at 12:53 PM ru  wrote:
>>>
 Dear Clojure users and team!
 Citation 1 from Clojure documentation:

 "Calling Clojure From Java..
 IFn plus = Clojure.var("clojure.core", "+"); 
 plus.invoke(1, 2);.."

 With functions all well but with def..

 Citation 2:

 "(def *symbol* *doc-string*? *init*?)

 Creates and interns or locates a global var 
  with the name of *symbol* and a 
 namespace of the value of the current namespace (*ns*). "

 I can not figure out how to get the value of the symbol, created with 
 def construct in Clojure and, for example, print it with 
 System.out.println 
 in Java.

 I tried everything, nothing works! :(

 Any help would be greatly appreciated.

 Sincerely,

   Ru





 -- 
 You received this message because you are subscribed to the Google
 Groups "Clojure" group.
 To post to this group, send email to clo...@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+u...@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+u...@googlegroups.com.
 To view this discussion on the web visit 
 https://groups.google.com/d/msgid/clojure/fcbd3883-705a-4180-9b99-7ccad64a09afn%40googlegroups.com
  
 
 .

>>>
>>>
>>> -- 
>>> With best wishes,Alex Ott
>>> http://alexott.net/
>>> Twitter: alexott_en (English), alexott (Russian)
>>>
>> -- 
>> You received this message because you are subscribed to the Google
>> Groups "Clojure" group.
>> To post to this group, send email to clo...@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+u...@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+u...@googlegroups.com.
>>
> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/clojure/ff4cb2f0-b121-4c54-b060-a1e64fbb0e4an%40googlegroups.com
>>  
>> 
>> .
>>
>

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/clojure/e7f0c619-25eb-4245-b039-cbeb344da0acn%40googlegroups.com.


Re: How to get a value of a var in Java

2021-06-18 Thread Gary Verhaegen
The official Clojure API for Java is defined here
 and
only supports importing Clojure vars under the IFn interface. In other
words, you can only import Clojure functions, not Clojure values, through
that API. What you are trying to do is not supported.

That does not mean it's impossible. If you control the Clojure side, the
easiest approach is to just make a function and call that through the
official API. If for some reason that's not an option, you can try gen-class
, which may be faster.

If you can't change the Clojure side, you'll have to use undocumented APIs.
The Clojure.var

call
will return a Var

(cast
to the IFn

interface), which has a deref()

method that should get you the underlying value. This is undocumented so it
may break and all that, but it really looks like all you need to do is cast
that IFn back to a Var (or an IDeref
,
as Chris suggested).

Overall, I'd strongly recommend going through the documented API and just
add a no-arg function.

On Fri, 18 Jun 2021 at 12:53, ru  wrote:

> Dear Clojure users and team!
> Citation 1 from Clojure documentation:
>
> "Calling Clojure From Java..
> IFn plus = Clojure.var("clojure.core", "+");
> plus.invoke(1, 2);.."
>
> With functions all well but with def..
>
> Citation 2:
>
> "(def *symbol* *doc-string*? *init*?)
>
> Creates and interns or locates a global var
>  with the name of *symbol* and a
> namespace of the value of the current namespace (*ns*). "
>
> I can not figure out how to get the value of the symbol, created with def
> construct in Clojure and, for example, print it with System.out.println in
> Java.
>
> I tried everything, nothing works! :(
>
> Any help would be greatly appreciated.
>
> Sincerely,
>
>   Ru
>
>
>
>
>
> --
> 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.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/clojure/fcbd3883-705a-4180-9b99-7ccad64a09afn%40googlegroups.com
> 
> .
>

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/clojure/CAGqyAap%3D9hSErWXirxYAcp8uHVDc91LSJ5bQkKwOxFad6KjG_Q%40mail.gmail.com.


Re: How to get a value of a var in Java

2021-06-18 Thread ru
Thank you, Gary, for the comprehensive answer. I have a control over 
Clojure side, so I decide to add special functions to get values of needed 
vars.
Thanks to all for the help.

Best regards,
  Ru

пятница, 18 июня 2021 г. в 20:23:31 UTC+3, gary.ve...@gmail.com: 

> The official Clojure API for Java is defined here 
>  and 
> only supports importing Clojure vars under the IFn interface. In other 
> words, you can only import Clojure functions, not Clojure values, through 
> that API. What you are trying to do is not supported.
>
> That does not mean it's impossible. If you control the Clojure side, the 
> easiest approach is to just make a function and call that through the 
> official API. If for some reason that's not an option, you can try 
> gen-class , which may be 
> faster.
>
> If you can't change the Clojure side, you'll have to use undocumented 
> APIs. The Clojure.var 
> 
>  call 
> will return a Var 
> 
>  (cast 
> to the IFn 
> 
>  
> interface), which has a deref() 
> 
>  
> method that should get you the underlying value. This is undocumented so it 
> may break and all that, but it really looks like all you need to do is cast 
> that IFn back to a Var (or an IDeref 
> ,
>  
> as Chris suggested).
>
> Overall, I'd strongly recommend going through the documented API and just 
> add a no-arg function.
>
> On Fri, 18 Jun 2021 at 12:53, ru  wrote:
>
>> Dear Clojure users and team!
>> Citation 1 from Clojure documentation:
>>
>> "Calling Clojure From Java..
>> IFn plus = Clojure.var("clojure.core", "+"); 
>> plus.invoke(1, 2);.."
>>
>> With functions all well but with def..
>>
>> Citation 2:
>>
>> "(def *symbol* *doc-string*? *init*?)
>>
>> Creates and interns or locates a global var 
>>  with the name of *symbol* and a 
>> namespace of the value of the current namespace (*ns*). "
>>
>> I can not figure out how to get the value of the symbol, created with def 
>> construct in Clojure and, for example, print it with System.out.println in 
>> Java.
>>
>> I tried everything, nothing works! :(
>>
>> Any help would be greatly appreciated.
>>
>> Sincerely,
>>
>>   Ru
>>
>>
>>
>>
>>
>> -- 
>> You received this message because you are subscribed to the Google
>> Groups "Clojure" group.
>> To post to this group, send email to clo...@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+u...@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+u...@googlegroups.com.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/clojure/fcbd3883-705a-4180-9b99-7ccad64a09afn%40googlegroups.com
>>  
>> 
>> .
>>
>

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/clojure/33e8dc71-7199-4f14-86d6-97b549b3b6e0n%40googlegroups.com.