Jason
Jason

Reputation: 12333

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

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?

Upvotes: 3

Views: 416

Answers (1)

Davyzhu
Davyzhu

Reputation: 1109

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.

Upvotes: 5

Related Questions