Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I perform Type Conversion in Clojure?

Tags:

clojure

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

like image 207
unj2 Avatar asked Jul 23 '09 19:07

unj2


People also ask

How is type conversion done?

In explicit type conversion, the user can typecast to convert a variable of one type to another data type. In C++, explicit type conversion can be accomplished in two ways: Conversion using the cast operator, and. Conversion using the assignment operator.

What is type conversion with example?

In computer science, type conversion, type casting, type coercion, and type juggling are different ways of changing an expression from one data type to another. An example would be the conversion of an integer value into a floating point value or its textual representation as a string, and vice versa.

What is the difference between typecasting and type conversion?

1. In type casting, a data type is converted into another data type by a programmer using casting operator. Whereas in type conversion, a data type is converted into another data type by a compiler.

What are the two types of type conversion?

There are two types of conversion: implicit and explicit. The term for implicit type conversion is coercion. Explicit type conversion in some specific way is known as casting.


1 Answers

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

like image 150
Arthur Ulfeldt Avatar answered Nov 02 '22 19:11

Arthur Ulfeldt