On Mon, Sep 21, 2009 at 5:22 PM, sross <ros...@gmail.com> wrote:
>
> Hi All,
>
>  I'm looking for a bit of advice for calling a java method which has a
> few different signatures (such as Connection.prepareStatement).
>  Is there a cleaner way of passing a variable number of arguments to
> the method, such as
>
> (apply (memfn prepareStatement) (sql/connection) args)
>
>  or is doing something like the  following the only approach.
>
> (defn prepare-statement
>  ([sql] (.prepareStatement (sql/connection) sql))
>  ([sql arg] (.prepareStatement (sql/connection) sql arg))
>  ([sql arg arg2] (.prepareStatement (sql/connection) sql arg arg2))
>  ([sql arg arg2 arg3] (.prepareStatement (sql/connection) sql arg
> arg2 arg3)))

Your solution will provide the best performance (you may need
to type-hint the args).  If performance doesn't matter, you
can use Rich's jcall fn:

  (defn jcall [obj name & args]
    (clojure.lang.Reflector.invokeInstanceMethod
      obj
      (str name)
      (if args (to-array args) (clojure.lang.RT.EMPTY_ARRAY))))

Then you could:

  (defn prepare-statement [& args]
    (apply jcall (sql/connection) "prepareStatement" args))

Note that uses runtime reflection, array creation, etc. each
time it's called.  You can expect it to be at least an order
of magnitude slower than if it were directly compiled like
your original solution could be.

--Chouser

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to