On Wed, May 5, 2010 at 10:32 AM, Sean Devlin <[email protected]>wrote:
> And I was too quick to post. Sorry about that.
>
> You've got the unchecked addition fn for speed, and those are allowed
> the throw overflow errors. The + fn is always supposed to work. The
> fact that it auto-promotes the bound version is proof. It needs to
> work with the literal, too.
>
> It's a Clojure issue, not a JVM one. That's why this needs to be
> fixed.
>
> Sean
Primitive types are only preserved in let bindings. From what I can tell the
primitive int gets promoted to an Integer when placed in a var. + is not
auto promoting, storing the primitive in a var is auto promoting. Thus +
works fine in the first case. In the second case Integer/MAX_VALUE is a Java
static method so it's primitive return type is known - int, thus the
overflow error. For example:
(def a (int 1))
(def b (int 2))
; slow generic math
(dotimes [_ 10]
(time
(dotimes [_ 1000000]
(+ a b))))
; let binding preserves primitive type
; fast math
(let [a (int 1)
b (int 2)]
(dotimes [_ 10]
(time
(dotimes [_ 1000000]
(+ a b)))))
So the following will also throw an overflow exception:
(def max-int Integer/MAX_VALUE)
(+ (int max-int) (int max-int))
So this seems like programmer error to me, but again that's because what
returns primitive types and where primitive types are preserved is not
totally intuitive (though pretty well documented I think)
David
--
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