测试对象是否是Clojure中的一个Java基本数组 [英] Testing whether an object is a Java primitive array in Clojure

查看:118
本文介绍了测试对象是否是Clojure中的一个Java基本数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是检测一个对象是否是Clojure中的一个Java基本类型数组的最佳方式?

What's the best way to detect whether an object is a Java primitive array in Clojure?

我需要这样做的原因是为了做基本数组一些特殊的处理,这可能会是这样:

The reason I need this is to do some special handling for primitive arrays, which might look something like:

  (if (byte-array? object)
    (handle-byte-array object))

这是在code的相当性能敏感的一块,所以我宁愿避免反光,如果在所有可能的。

It's in a fairly performance-sensitive piece of code so I'd rather avoid reflection if at all possible.

推荐答案

您可以使用反射一次,从名字,这个缓存,然后拿到类
其余比较了

you can use reflection once to get the class from the name, cache this and then compare the rest to that

(def array-of-ints-type (Class/forName "[I"))
(def array-of-bytes-type (Class/forName "[B")) 
...

(= (type (into-array Integer/TYPE [1 3 4])) array-of-ints-type)
true

这篇关于测试对象是否是Clojure中的一个Java基本数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆