Mark,

In case you were asking how to call a Clojure function from Java  
without wrapping it, check out clojure.lang.IFn and AFn.

For example, say you prefer to write a class in Java but need to  
accept a Clojure fn as an argument in
one of the class methods:

public int callFromClojure(IFn clojureFn) {
   ...
   Object result = clojureFn.invoke(arg1, arg2);
   ...
}

That's just a rough example and IFn may not be the correct abstraction  
to use.

-Matt

On Feb 13, 2009, at 2:19 PM, Chas Emerick wrote:

>
> Mark,
>
> If you use the #^{:static true} metadata on a :methods definition in a
> gen-class spec for your class' implementing namespace, then those fns
> appear as static functions in the generated Java class:
>
> Clojure:
>
> (ns bar.Foo
>   (:gen-class
>     :methods [#^{:static true} [stringLength [String] int]]))
>
> (defn -stringLength
>   [s]
>   (.length s))
>
> ----------
> compile, then in Java:
>
> bar.Foo.stringLength("blah") => 4
> --------
>
> You can use this to gracefully expose clojure fns as Java static
> methods (e.g. the implementation of -stringLength above could just
> delegate to a more idiomatically-named clojure fn like string-length
> so that the fn can be pleasantly accessed by clojure and Java
> clients).  Of course, you can access instance methods of classes
> written in clojure as well, but that requires also adding a
> constructor spec to :gen-class and tracking whatever state is
> appropriate for the class/method in question.
>
> - Chas
>
> On Feb 13, 2009, at 12:29 PM, Mark Volkmann wrote:
>
>>
>> On Fri, Feb 13, 2009 at 11:19 AM, Laurent PETIT <laurent.pe...@gmail.com
>>> wrote:
>>> http://en.wikibooks.org/wiki/Clojure_Programming/Tutorials_and_Tips#Invoking_Clojure_from_Java
>>>
>>> 2009/2/13 Mark Volkmann <r.mark.volkm...@gmail.com>
>>>>
>>>> Can someone point me to documentation on how to invoke Clojure
>>>> functions from Java?
>>
>> Thanks! That's interesting, but what I really want to do is call
>> Clojure functions that have already been compiled into .class files
>> from Java. Have you seen an example of that anywhere? I don't see it
>> on that page.
>>
>> -- 
>> R. Mark Volkmann
>> Object Computing, Inc.
>>
>>>
>
>
> >


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