4

I know that I implement a Java interface in Clojure using proxy like this:

(def print-element-handler
  (proxy [DefaultHandler] []
    (startElement [uri local qname atts]
      (println (format "Saw element: %s" qname)))))

Note that there are four args, [uri local qname atts], for the four args in the interface method, startElement.

Suppose a method in a Java interface has a variable number of args like this:

List<Task> getTasks(Object... args);

What do I put for the arg list in the corresponding Clojure function?

1 Answer 1

4

I am not 100% certain, as I am not able to test it out at the moment, but I believe the answer is you just have a single parameter for the array. I know for a fact that when you call a Java method with varags, you have to convert the Clojure collection into an array before passing it in. I imagine it's no different here.

For an example, here is the source for format:

(defn format
  "Formats a string using java.lang.String.format,
   see java.util.Formatter for format string syntax"
  {:tag String
   :added "1.0"}
  [fmt & args]
  (String/format fmt (to-array args)))
Sign up to request clarification or add additional context in comments.

1 Comment

Right. So you do something like (reify TheInterface (getTasks [this tasks] (doseq [task tasks] (...)))). The Object[] that tasks really is, at the bytecode level, is just a single parameter. Then in the implementation you treat it like any other seq.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.