On Mon May 13 13:36:53 2013, Mark Engelberg wrote:
What tools exist in Clojure for understanding whether a given variable
is boxed or primitive?
To get a better handle on boxing, I started playing around with things
like:
(def x 1)
(def ^int x 1)
(def x (int 1))
I want to know how Clojure is storing that 1 in the various cases.
The only tool I know of to investigate the type of an object is to
call (type x), but type always reports things as boxed, so it is of no
help whatsoever.
How does one go about investigating this?
To be clear, I care less about the actual (def x ...) examples above
(although if you want to comment about that, that's certainly
helpful). I'm mostly concerned with figuring out how I can explore
these issues on my own.
Thanks,
Mark
--
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient
with your first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.
In the past I have used this code to help verify that functions are
returning primitives:
(definterface IPrimitiveTester
(getType [^int x])
(getType [^long x])
(getType [^float x])
(getType [^double 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 ^Object x] :object))
(defmacro primitive-type [x]
`(.getType (PrimitiveTester.) ~x))
(defmacro pt [x]
`(.getType (PrimitiveTester.) ~x))
;; example use
user=> (defn foo ^long [] 23)
#'user/foo
user=> (pt (foo))
:long
However, I just tried it on your x and I got:
user=> (def ^int x 12)
#'user/x
user=> (pt x)
CompilerException java.lang.IllegalArgumentException: Unable to resolve
classname: clojure.core$int@1c461984, compiling:(NO_SOURCE_PATH:1)
Hopefully this helps somewhat... I'd be curious if anyone else has a
good way of doing this.
-Ben
--
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.