简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM