我已阅读 Calling clojure from java 的精彩答案它展示了从 Java 调用 Clojure 的新风格。
但是给出的示例仅返回一个 float 。如何将 Clojure vector 和其他序列类型从 Clojure 返回到 Java?还有 map ?
特别是在这一代中:
(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]
] ))
XXXX、YYYY 和 ZZZZ 应该填什么?它们在我的 Java 程序中应该是什么类型?
更新:为了回应 Tomo 的有希望的答案,如果我尝试在 gen-clas 中使用 IPercientVector ,我会得到 p>
Caused by: java.lang.ClassNotFoundException: java.lang.IPersistentVector
当我尝试创建 Uberjar 时。我想我还需要做一些其他事情来导入这些类型?
更新 2:好的,感谢 Tomo 的更多评论,这似乎有效:
(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]
] ))
最佳答案
我会使用适当的接口(interface):IPercientMap
、IPercientVector
、ISeq
等。查看源代码(https://github.com/clojure/clojure/tree/master/src/jvm/clojure/lang)。
关于java - 将 vector 、序列和映射从 Clojure 返回到 Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27040442/