是否可以在 Clojure 中声明一个可变参数函数,该函数可以作为 Java 的可变参数方法调用?
考虑一些正在开发的代码的摘录:
(ns com.mydomain.expression.base
(:gen-class
:name com.mydomain.expression.Base
:methods [^:static [exprFactory [String String ?????] Object]]
)
(defn expr-factory
; worker function that is also called from Clojure
[id func & args]
(let [ex ("construction code here")]
ex))
(defn -exprFactory
; interface method called from Java
[idStr funcStr & argsArray]
(apply expr-factory idStr funcStr (seq argsArray)))
有什么我可以代替的,?????
以允许 Java 调用该exprFactory
方法并知道它是一个 varargs 方法:
import com.mydomain.expression.Base;
...
Object e1 = Base.exprFactory("e1", "day");
Object e2 = Base.exprFactory("e2", "length", "string");
Object e3 = Base.exprFactory("e3", "*", 4, 5);
Object sum = Base.exprFactory("sum", "+", e1, e2, e3);
为了使这一点更清楚一点,我知道我使用Object
代替?????
并更改exprFactory
为:
(defn -exprFactory
; interface method called from Java
[idStr funcStr argsArray]
(apply expr-factory idStr funcStr (seq argsArray)))
..但这意味着我必须像这样编写 Java 调用:
import com.mydomain.expression.Base;
...
Object e1 = Base.exprFactory("e1", "day", new Object[0] ));
Object e2 = Base.exprFactory("e2", "length", new Object[] { "string" }));
Object e3 = Base.exprFactory("e3", "*", new Integer[] { 4, 5 }));
Object sum = Base.exprFactory("sum", "+", new Object[] { e1, e2, e3 }));
同样,我知道我可以在 Java 中编写一个调用 non-variadic 的 varargs 包装器方法exprFactory
,但如果可能的话,我想避免这种情况。