如何在Clojure中执行类型转换?

时间:2022-03-30 22:01:19

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

如何在Clojure中将Symbol转换为String,Integer转换为Float以及其他类似类型的转换?

1 个解决方案

#1


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

有关更多信息:http://clojure.org/java_interop#toc35

#1


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

有关更多信息:http://clojure.org/java_interop#toc35