What’s behind the NumericRange Int size restriction on the Scala for-loop comprehension? Is it possible (without to much headache) to extend “for/Seqs” NumericRange to make use of Long (or anything bigger than Int.MaxValue)?
scala>for (i: Long <- 0L to 10000000000) {}
java.lang.IllegalArgumentException: 0 to 10000000000L by 1: "seqs cannot contain more than Int.MaxValue elements." at scala.collection.immutable.NumericRange$.count(NumericRange.scala:227) at scala.collection.immutable.NumericRange.numRangeElements(NumericRange.scala:53) at scala.collection.immutable.NumericRange.length(NumericRange.scala:55) at scala.collection.immutable.NumericRange.foreach(NumericRange.scala:73) at .<init>(<console>:19) at .<clinit>(<console>) at .<init>(<console>:11) at .<clinit>(<console>) at $print(<console>) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:704) at scala.tools.nsc.interpreter.IMain$Request$$anonfun$14.apply(IMain.scala:920) at scala.tools.nsc.interpreter.Line$$anonfun$1.apply$mcV$sp(Line.scala:43) at scala.tools.nsc.io.package$$anon$2.run(package.scala:25) at java.lang.Thread.run(Thread.java:680)
—
Thanks in advance!
Short answer- it appears to be a “feature” – at least, it’s working as designed.
As @drexin pointed out, the implementation of “to” is limited to having an Int range. However…
The problem is that NumericRange[T].count(), .numRangeElements, and .length() returns an Int – regardless of what T is. In this case, it’s a NumericRange[Long], where it seems a bit wrong to have count() limited to 31 bits, IMHO.
However…
From browsing Jira issues, this appears to be working as designed. See, e.g., SI-4370. But just to be sure it’s been thought out from this perspective, I entered SI-5619.