Oops, I meant "Clojure should box primitive ints as Integers." :-)
On Oct 20, 12:15 pm, nathanmarz <nathan.m...@gmail.com> wrote: > Thanks, that clarifies the behavior. Regardless though, at some point > the "int" is becoming a "Long" which is a change of type. I'm arguing > that Clojure should box primitive ints as Longs. > > Stu, I wouldn't say Clojure's behavior makes it "just work". For > example, if I obtained by number using Integer/valueOf, then Clojure > will not change the Integer to a Long and will not prevent me from > putting it in a collection. It's confusing that Integer/valueOf will > stay an Integer in Clojure-land, and Integer/parseInt will become a > Long in Clojure-land. > > The use case I'm interested in here is just this one point of Java > interop: what Clojure does with primitive ints that it gets from a > Java object (as far as I can tell, this is the only way to get a > primitive int in Clojure 1.3). I think it's better that Clojure be > consistent in its treatment of Integer objects and primitive ints by > not changing the types on you. > > -Nathan > > On Oct 20, 10:19 am, Justin Kramer <jkkra...@gmail.com> wrote: > > > > > > > > > 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