Oops, I elided a little too much. Need a method with an Object signature to 
distinguish Integer from int:

(definterface IPrimitiveTester
  (getType [^int x])
  (getType [^long x])
  ;; etc
  (getType [^Object x]))

(deftype PrimitiveTester []
  IPrimitiveTester
  (getType [this ^int x] :int)
  (getType [this ^long x] :long)
  ;; etc
  (getType [this ^Object x] :object))

(defmacro primitive-type [x]
  `(.getType (PrimitiveTester.) ~x))

(comment

  user=> (primitive-type (Integer. 5))       
  :object
  user=> (primitive-type (Integer/parseInt "5"))
  :int

  )


On Thursday, October 20, 2011 1:13:03 PM UTC-4, Justin Kramer wrote:
>
> Here's a quick proof using an interface-based primitive detector:
>
> (definterface IPrimitiveTester
>   (getType [^int x])
>   (getType [^long x])
>   ;; other types elided
>   )
>
> (deftype PrimitiveTester []
>   IPrimitiveTester
>   (getType [this ^int x] :int)
>   (getType [this ^long x] :long)
>   ;; other types elided
>   )
>
> (defmacro primitive-type [x]
>   `(.getType (PrimitiveTester.) ~x))
>
> (comment
>
>   user=> (primitive-type 5) ;unboxed
>   :long
>   user=> (primitive-type (Integer/parseInt "5")) ;unboxed
>   :int
>   user=> (class (Integer/parseInt "5")) ;boxed
>   java.lang.Long
>
>   )
>
> Justin
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to