Testing whether an object is a Java primitive array in Clojure

前端 未结 7 2424
时光说笑
时光说笑 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条回答
  •  萌比男神i
    2021-02-08 09:23

    As pointed by Arthur Ulfeldt, you can use Class/forName, for example, like here:

    (def byte_array_class (Class/forName "[B"))
    
    (defn byte-array? [arr] (instance? byte_array_class arr))
    

    If you want to avoid magic strings like "[B" when caching the classes, you can apply class to an existing array object:

    (def byte_array_class (class (byte-array [])))
    

提交回复
热议问题