Welcome! Please see the About page for a little more info on how this works.

0 votes
in Clojure by

Was using an int a deliberate design choice or a miss?

www.server=> (count (range 0 2147483647))
www.server=> (count (range 0 2547483647))
ArithmeticException integer overflow  clojure.lang.Numbers.throwIntOverflow 

1 Answer

+2 votes

Yes, it was deliberate - Clojure follows Java, which uses int-based counts (and we're kind of restricted to that for the size() method impls in collections due to the Java interfaces). They've been talking about ways to push past this limit in Java for a few years too (particularly for arrays), but it's baked into a lot of places.

The last time I worked on range actually, I looked into it a bit, and the plan is to eventually make the count function long-based instead. This looks feasible to do and should be a non-breaking change in most cases, but I haven't done a serious analysis yet. The persistent collection size() methods would still be int-based, but we could have our own methods that are long based instead and have count use those instead.

There is a ticket tracking this at https://clojure.atlassian.net/browse/CLJ-1729 (and here in ask at https://ask.clojure.org/index.php/1527/make-counted-and-count-return-long-instead-of-integer). In practice, it's pretty rare to have more than Integer.MAX_VALUE items in a collection, so it's rarely been a real issue.