使用Java中的named / default args调用Clojure函数的最佳方法

时间:2021-04-11 22:49:22

When calling a Clojure function from Java, what's the best way to specify named arguments to the function?

从Java调用Clojure函数时,为函数指定命名参数的最佳方法是什么?

I have the following function:

我有以下功能:

(defn myFn [a b & {:keys [c d] :or {c 1 d 2}}]
   ; do something
   )

And I currently call it from Java with lines like this:

我现在用Java来调用它,如下所示:

IFn myFn = Clojure.var("my.namespace", "myFn");
myFn.invoke(5, 6, Clojure.read(":c"), 7, Clojure.read(":d"), 8);

I find the Clojure.read... parts of the above statement verbose. Is there a simpler way to make this call?

我发现Clojure.read ...以上语句的部分内容详细。有没有更简单的方式来打这个电话?

1 个解决方案

#1


5  

The question is not about how to pass named argument, but how to create keywords in Java code:

问题不是如何传递命名参数,而是如何在Java代码中创建关键字:

import clojure.lang.Keyword;
// others omitted...
myFn.invoke(5, 6, Keyword.find("c"), 7, Keyword.find("d"), 8);

Clojure.read would be considered too cumbersome for the task and too dangerous as it can read in any code.

对于任务来说,Clojure.read会被认为太麻烦,而且任何代码都可以读取它太危险。

#1


5  

The question is not about how to pass named argument, but how to create keywords in Java code:

问题不是如何传递命名参数,而是如何在Java代码中创建关键字:

import clojure.lang.Keyword;
// others omitted...
myFn.invoke(5, 6, Keyword.find("c"), 7, Keyword.find("d"), 8);

Clojure.read would be considered too cumbersome for the task and too dangerous as it can read in any code.

对于任务来说,Clojure.read会被认为太麻烦,而且任何代码都可以读取它太危险。