On Tue, Apr 20, 2010 at 04:43:13AM -0700, jvshahid wrote: > Thanks Per, > > This definitely works > > > Try (loop [x (. 1 longValue)] (if (= 0 x) x (recur (- x (long 1))))). > > but from my understanding of clojure internals clojure.lang.Numbers > should take care of that. Since one of the arguments of '-' is a long > a LongOps should do the minus and return a long. Which leads me to the > ask why the first version doesn't work when it should.
I think that the problem is that in (- x 1), even though x is a primitive long, 1 is an Integer (non-primitive). That is why you are able to do (. 1 longValue), it treats 1 as an Integer object. Therefore, x is boxed into a Long (non-primitive) and 1 is promoted to a Long. So, the result of the operation is a Long. However, in the loop/recur you have created, the x in loop is a primitive, and recur is unable to automatically unbox the Long into a long. On the other hand (dec x) only has one argument, a long. So, I am guessing that dec has the ability to decrement a primitive without any boxing, so it naturally returns a primitive long. In most Clojure code that depends on retaining primitives, numeric constants are coerced into primitives, like (int 1). Sincerely, Daniel Solano Gómez
pgpGy8AxHEMJE.pgp
Description: PGP signature