Java vararg functions called from Clojure

880 views
Skip to first unread message

Tom Emerson

unread,
Dec 8, 2008, 1:34:36 PM12/8/08
to clo...@googlegroups.com
Calling Java vararg functions seems to be less transparent in Clojure interop:

(String/format "%03d" 5)

java.lang.ClassCastException: java.lang.Integer cannot be cast to
[Ljava.lang.Object; (NO_SOURCE_FILE:0)
[Thrown class clojure.lang.Compiler$CompilerException]

while

(String/format "%03d" (to-array '(5)))

works as you would expect.

I just wanted to make sure I wasn't missing something. Perhaps
something along the lines of

(defn format-string
[fmt & args]
(String/format fmt (to-array args)))

would be useful in string-utils.

-tree

--
Tom Emerson
trem...@gmail.com
http://www.dreamersrealm.net/~tree

Stephen C. Gilardi

unread,
Dec 8, 2008, 1:53:58 PM12/8/08
to clo...@googlegroups.com

On Dec 8, 2008, at 1:34 PM, Tom Emerson wrote:

I just wanted to make sure I wasn't missing something. Perhaps
something along the lines of

(defn format-string
 [fmt & args]
 (String/format fmt (to-array args)))

Hi Tom,

Good call on it being a convenient function to have around. It's available as clojure.core/format. There's also printf along the same lines.

--Steve

Randall R Schulz

unread,
Dec 8, 2008, 2:05:44 PM12/8/08
to clo...@googlegroups.com

Correct me if I'm wrong or insufficiently aware of the relevant issues,
but is it not the case that such a helper function has an easier job
for Format since it wants an array of Object. In other contexts, you
have to know what the type of the array elements are to construct an
acceptable varargs array.

So would it not be the case that to be generic a varargs array-builder
would have to use reflection on the target method?


> --Steve


Randall Schulz

Tom Emerson

unread,
Dec 8, 2008, 3:11:12 PM12/8/08
to clo...@googlegroups.com
On Mon, Dec 8, 2008 at 1:53 PM, Stephen C. Gilardi <sque...@mac.com> wrote:
> Good call on it being a convenient function to have around. It's available
> as clojure.core/format. There's also printf along the same lines.

Steve, that is probably the most politely worded, "Read the API docs
you git!" I've come across. Thanks for pointing the obvious to me. :)

Stephen C. Gilardi

unread,
Dec 8, 2008, 6:51:23 PM12/8/08
to clo...@googlegroups.com

On Dec 8, 2008, at 2:05 PM, Randall R Schulz wrote:

> Correct me if I'm wrong or insufficiently aware of the relevant
> issues,
> but is it not the case that such a helper function has an easier job
> for Format since it wants an array of Object. In other contexts, you
> have to know what the type of the array elements are to construct an
> acceptable varargs array.

I was addressing "format" itself. In the case of any (manually
written) Clojure wrapper for a Java variadic method, you would know
the required type and could ensure it using:

(into-array the-type args).

> So would it not be the case that to be generic a varargs array-builder
> would have to use reflection on the target method?

Reflection to determine the array type seems necessary to accomplish
that.

Another issue is how to distinguish a single argument at the varargs
position between:

an array being passed as a single varargs argument itself, and
an array being passed containing the varargs arguments.

Java looks for a type match to the array type that corresponds to the
varargs param type (e.g., Foo... -> Foo[]) to distinguish. I think
Clojure could do the same.

--Steve

Reply all
Reply to author
Forward
0 new messages