Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to call Clojure function with named/default args from Java

Tags:

java

clojure

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

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:

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?

like image 892
Jason Avatar asked Feb 18 '26 19:02

Jason


1 Answers

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

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.

like image 79
Davyzhu Avatar answered Feb 21 '26 15:02

Davyzhu