(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
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)))
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
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. :)
> 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