Skip to content

Instantly share code, notes, and snippets.

@jkk
Created September 19, 2011 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkk/1226968 to your computer and use it in GitHub Desktop.
Save jkk/1226968 to your computer and use it in GitHub Desktop.
(definterface IPrimitiveTester
(getType [^int x])
(getType [^long x])
(getType [^float x])
(getType [^double x])
(getType [^byte x])
(getType [^short x])
(getType [^char x])
(getType [^boolean x])
(getType [^Object x]))
(deftype PrimitiveTester []
IPrimitiveTester
(getType [this ^int x] :int)
(getType [this ^long x] :long)
(getType [this ^float x] :float)
(getType [this ^double x] :double)
(getType [this ^byte x] :byte)
(getType [this ^short x] :short)
(getType [this ^char x] :char)
(getType [this ^boolean x] :boolean)
(getType [this ^Object x] :object))
(defmacro primitive-type [x]
`(.getType (PrimitiveTester.) ~x))
(defmacro primitive? [x]
`(not= :object (primitive-type ~x)))
(comment
;; Clojure 1.2
(primitive? 1) ;=> false
(primitive-type 1) ;=> :object
(primitive? (Math/pow 2 2)) ;=> true
(primitive? (* 2 (Math/pow 2 2))) ;=> false
;; Clojure 1.3
(primitive? 1) ;=> true
(primitive-type 1) ;=> :long
(primitive? (Math/pow 2 2)) ;=> true
(primitive? (* 2 (Math/pow 2 2))) ;=> true
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment