Testing whether an object is a Java primitive array in Clojure

前端 未结 7 2411
时光说笑
时光说笑 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:05

    (defn primitive-array? [o]
      (let [c (class o)]
        (and (.isArray c)
             (.. c getComponentType isPrimitive))))
    

    For particular cases, you could use something like the following:

    (defn long-array? [o]
      (let [c (class o)]
        (and (.isArray c)
             (identical? (.getComponentType c) Long/TYPE))))
    

提交回复
热议问题