To force a value to represent a certain data type, you'll have to cast the values as it's done in most modern languages. It's pretty straightforward like everything else in Clojure:
user> (format "%s = %d" (type \A) (int \A)) "class java.lang.Character = 65" user> (char 0x41) \A user> (= 0x41 (byte 0x41) (int \A)) true
Sometimes, casting values can come at the price of affecting the performance. Notice the time elapsed on casting the value 1.0
:
user> (time (short 1.0)) "Elapsed time: 0.073201 msecs" 1 user> (time (int 1.0)) "Elapsed time: 0.07077 msecs" 1 user> (time (long 1.0)) "Elapsed time: 0.070731 msecs" 1
Some languages cast string values to integers, but Clojure isn't one of them. Java interoperation methods are required to convert a string to an integer:
user> (int "123") ClassCastException java.lang.String cannot be cast to java.lang.Character user> (time (Integer/parseInt "123")) "Elapsed time: 0.145911 msecs" 123 user> (time (Integer. "123")) "Elapsed...