Hi Blake,

Also, I presume that a Closure program has immediate access to all Java 
> classes and methods without any work.  Is that true?


Clojure embraces the JVM by providing a number of interoperability features 
<http://clojure-doc.org/articles/language/interop.html> that allow you to 
do almost anything (if not anything) you can do in Java. You can even 
create Java classes or interfaces with the gen-class 
<http://clojuredocs.org/clojure.core/gen-class> and gen-interface 
<http://clojuredocs.org/clojure.core/gen-interface> macros. 

Among these features there is a special *.* (dot) operator for accessing 
field or method members. Even though the result of using reads almost like 
a Clojure function call, it's actually a special interoperability 
construct. The split 
<http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#split-java.lang.String->
 method 
of a Java String would be called like this *(.split "Hello World!" " ")* for 
the single argument signature and like this *(.split "Hello World!" " " 1)* for 
the one with two arguments. 

It recently occurred to me that the author of Closure probably took that 
> problem into account when creating Closure.  So, Closure would be able to 
> call Java methods as if they were Closure functions taking into account the 
> argument signature.  Does Closure do that?


The use of a method like indexOf 
<http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#indexOf-int-> 
that accepts a single argument of either type* String* or* int*, is 
resolved automatically in Clojure. If the type of the argument doesn't 
match an existing method, an exception is thrown. 

*(.indexOf "Hello World!" "Wo")*
*;;= 6*
*(.indexOf "Hello World!" (int \!))*
*;;= 11*

*(.indexOf "Hello World!" 2.0)*
*;; IllegalArgumentException No matching method found: indexOf for class 
java.lang.String *

*;; clojure.lang.Reflector.invokeMatchingMethod (Reflector.java:80) *


The *.instanceMember* syntax is special though, it is not treated as a 
regular Clojure function and is resolved at compile time. This means for 
example that you can't use partial 
<http://clojuredocs.org/clojure.core/partial> with instance methods. If you 
wanted to create a Clojure function equivalent to calling the *split *method 
of any arity you would have to wrap it up in a function with multiple 
arities.

*(defn split [s]*
*  ([regex]*
*    (.split s regex))*
*  ([regex limit]*
*    (.split s regex limit)))*


Some Clojure libraries are actually wrappers around Java libraries, but 
they provide a more idiomatic API for the Clojure programmer. Seesaw for 
example "*Seesaw turns the Horror of Swing into a friendly, 
well-documented, Clojure library*".

I hope I understood your questions correctly and I was able to express 
myself clearly.

Cheers!

Juan

On Friday, November 7, 2014 11:32:00 PM UTC-3, Blake McBride wrote:
>
> Greetings,
>
> I am an old lisp guy that is new to Clojure.  At one time I wanted to use 
> ABCL as a scripting language for a large, web-based Java app.  (ABCL is 
> Common Lisp written in for the JVM.)  The integration and operation of it 
> was easy and nice.  I did, however, run into an insurmountable problem.  
>
> I created some clue code that, through reflection of a class, discovered 
> Java methods and created a CLOS mirror.  Worked well.  The problem I ran 
> into is that Java allows more than one method to have the same name 
> provided it has a different argument signature.  Lisp can't do that without 
> a real lot of work (make generic functions evaluate the arguments and then 
> execute the applicable method).  It appeared as a big job to me to have 
> Lisp automatically generate all that code for each Java method.  I 
> eventually kind of gave up.
>
> It recently occurred to me that the author of Closure probably took that 
> problem into account when creating Closure.  So, Closure would be able to 
> call Java methods as if they were Closure functions taking into account the 
> argument signature.  Does Closure do that?
>
> Also, I presume that a Closure program has immediate access to all Java 
> classes and methods without any work.  Is that true?
>
> Thanks.
>
> Blake McBride
>
>

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