5

The following call:

(String/format "%s" "a")

... throws a "ClassCastException java.lang.String cannot be cast to [Ljava.lang.Object" exception in Clojure.

(String/format "%s" (cast Object "a"))

.... produces the same exception.

2
  • 1
    Any reason you need to use String.format and not Clojure.core/format - it wraps with to-array for you? (format "%s" "a") Commented Jan 14, 2013 at 16:55
  • No reason to use String.format in particular, just being curious ... Commented Jan 14, 2013 at 17:09

3 Answers 3

14

Because last argument in java API is array Object[] instead of Object.

Just call (String/format "%s" (into-array ["a"]))

But more idiomatic to use (format "Hello %s" "world")

Sign up to request clarification or add additional context in comments.

Comments

4

[L means "I want array" so use (String/format "%s" (to-array "a")). General syntax: (to-array ["a" 42 0.666]).

Comments

4

String.format is using varargs, which are internally plain Java arrays. You then need to convert the input parameters into an array, e.g. by using to-array:

user=> (String/format "%s" (to-array "a"))
"a"

Comments

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.