0

I've read the excellent answer to Calling clojure from java which shows the new style of calling Clojure from Java.

But the example given just returns a float. How can I return Clojure Vectors and other Sequence types from Clojure to Java? And Maps?

Specifically in this gen-class :

(ns dummy-app.core
  (:gen-class
   :name com.example.dummy
   :methods [#^{:static true} [f [int] int]
             #^{:static true} [getVect [] XXXX]
             #^{:static true} [getMap [] YYYY]
             #^{:static true} [getSeq [] ZZZZ]
             ] ))

what should I put for XXXX, YYYY and ZZZZ? And what types should they be in my Java program?

Update : in response to Tomo's promising answer, if I try to just use IPersistentVector in the gen-clas I get

Caused by: java.lang.ClassNotFoundException: java.lang.IPersistentVector

when I try to create the Uberjar. I assume there's something else I need to do to import these types?

Update 2 : OK, thanks to more comments from Tomo this seems to work :

(ns dummy-app.core
  (import clojure.lang.IPersistentVector)
  (:gen-class
   :name com.example.dummy
   :methods [#^{:static true} [f [int] int]
             #^{:static true} [getVect [] clojure.lang.IPersistentVector]
             ] ))
Community
  • 1
  • 1
interstar
  • 26,048
  • 36
  • 112
  • 180

2 Answers2

1

I'd use appropriate interfaces: IPersistentMap, IPersistentVector, ISeq and so on. Check out the source code (https://github.com/clojure/clojure/tree/master/src/jvm/clojure/lang).

Tomo
  • 3,431
  • 5
  • 25
  • 28
0

You can also just specify the appropriate Java interfaces.

Clojure vectors, list and seqs all implement java.util.List

Clojure maps implement java.util.Map

Functions implement both java.util.concurrent.Callable and java.lang.Runnable

DanLebrero
  • 8,545
  • 1
  • 29
  • 30