Range size limitation

94 views
Skip to first unread message

Matt McClellan

unread,
May 29, 2013, 3:01:44 PM5/29/13
to scala...@googlegroups.com
When trying to create a very large Range lets say from 1L to Long.MaxValue you get this error:
java.lang.IllegalArgumentException: 1 to 9223372036854775807 by 1: seqs cannot contain more than Int.MaxValue elements.

However, I can do Stream.from(1) which is infinite size and thereby contradicts this error message that seqs cannot have more than Int.MaxValue elements.  I think this is because a Range is an IndexedSeq which has this size limitation, whereas a Stream is a LinearSeq?

I'm also curious why there is a limit, if your dealing with numeric ranges 1 to Infinite is just as valid as 1 to 5, so why limit the size of them at all?

√iktor Ҡlang

unread,
May 29, 2013, 3:13:11 PM5/29/13
to Matt McClellan, scala-user
My guess: what would (1 to Long.MaxValue).size() return?

I.e. JVM collections are generally capped to Int.MaxValue for size.

Cheers,


--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Viktor Klang
Director of Engineering

Twitter: @viktorklang

Matt McClellan

unread,
May 29, 2013, 4:31:14 PM5/29/13
to scala...@googlegroups.com, Matt McClellan
That scenario isn't really too different from a very large Stream.  When testing Stream.size() on streams larger than Int.MaxValue I get either an out of memory exception or an infinite loop (depending on how much bigger it is).  

However, after thinking about it I understand the limits now, in order to fit in with java collections and access by index it needs to limit to Int.MaxValue.  My argument is that I don't believe a Range should be a collection.  

What I was originally looking for when trying to use it was something that was more mathematical IE (1 to 5) contains 1.111111112.  I'd say the scala Range class is more of a Set as it stands currently.  In my opinion many of the functions in the Range class should return a Set instead of a Range for example: 1 to 5 by 2 isn't a Range its the set of numbers {1, 3, 5} derived from the range 1 to 5.
Reply all
Reply to author
Forward
Message has been deleted
0 new messages