Is it possible to declare a variadic function in Clojure that can be called as a varargs method from Java?
Consider this extract from some code under development:
(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)))
Is there anything I can put in place of ????? to allow Java to call the exprFactory method and to know that it is a varargs method:
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);
To make this a little clearer, I know I use Object in place of ????? and change exprFactory to:
(defn -exprFactory
; interface method called from Java
[idStr funcStr argsArray]
(apply expr-factory idStr funcStr (seq argsArray)))
..but that means I have to write Java calls like this:
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 }));
Again, I know I could write a varargs wrapper method in Java that calls the non-variadic exprFactory, but I'd like to avoid that if possible.
String[]with this:"[Ljava.lang.String;"instead of?????. I tried that, it compiles, but eclipse won't recognized the varargs, it insists on aString[]even tho a java class and a gen-class class have identical type sigs. You might have more luck:methods [^:static [exprFactory [String String & Object] Object]]and have Clojure automatically handle the interpretation of the array as a sequence.