user468687
user468687

Reputation:

How to a cast to a String[] in Clojure?

How do I make this work?

user=> (Test/main (object-array ["hello" "world"]))

ClassCastException [Ljava.lang.Object; cannot be cast to [Ljava.lang.String;  user/eval21 (NO_SOURCE_FILE:21)

Upvotes: 12

Views: 3345

Answers (2)

ffriend
ffriend

Reputation: 28492

(def v ["Hello" "World"])
(def a (to-array v))

EDIT: As @Jeff noted, into-array will create typed array (String[]) instead of Object[].

Upvotes: 0

Jeff Foster
Jeff Foster

Reputation: 44706

object-array is just used to make arrays of type Object[]. Try into-array as you can specify the type (see here)

> (into-array String ["Awesome","Sauce"])
> #<String[] [Ljava.lang.String;@1b86d76f>

Upvotes: 20

Related Questions