Testing whether an object is a Java primitive array in Clojure

前端 未结 7 2280
时光说笑
时光说笑 2021-02-08 08:47

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

7条回答
  •  余生分开走
    2021-02-08 09:23

    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
    

提交回复
热议问题