unj2
unj2

Reputation: 53491

How do I perform Type Conversion in Clojure?

How do I convert Symbol to String, Integer to Float, and other similar type conversions in Clojure?

Upvotes: 17

Views: 16718

Answers (1)

Arthur Ulfeldt
Arthur Ulfeldt

Reputation: 91554

You tell the compiler what type you want something to be by adding metadata to it.
This can make some operations faster and help eliminate reflection. The ^symbol is syntactic sugar for add this to the metadata for whatever comes next.

(defn my-function  [^String my-string] ....

Symbol to string:

(str 'my-symbol)

For numbers, use the name of the type as a function name:

(int 4922354)
(double 42)
(byte 254)
(char 20)
etc...

For more info: http://clojure.org/java_interop#toc35

Upvotes: 30

Related Questions