But Clojure is already inconsistent. ints and Integers in interop are treated differently. The only way to make Clojure consistent is to either:
1. Box ints as Integers 2. Always convert Integers to Longs. I'm not sure on the feasibility of #2. I'm not trying to be obtuse, but I really don't see the benefit of boxing primitive ints as Longs given how Integer objects are treated. Right now, if you obtain an Integer object via interop and want it to be compatible with Clojure's regular numerics, you still have to manually convert that Integer object into a Long. What I'm proposing is that you treat primitive ints obtained via interop the exact same way, which avoids the weird type issues that I ran into. -Nathan On Oct 20, 12:26 pm, David Nolen <dnolen.li...@gmail.com> wrote: > Such a change would be make Clojure's numeric design inconsistent. You keep > saying that Clojure is changing the types - that's probably not the right > way to look at it. > > It's a semantic change, Clojure now only has 64bit primitives - the same way > that JavaScript only has doubles. > > Prior to the 1.3 change, the semantics gave you a free lunch around > primitive ints in the interop scenario. Now you have be explicit just as you > do with pretty much any kind of Java interop. > > David > > > > > > > > On Thu, Oct 20, 2011 at 3:16 PM, nathanmarz <nathan.m...@gmail.com> wrote: > > 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 -- 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