开发者

Restriction on Range

i开发者_运维问答'm surprised. Why was made restriction of implementation to type Range, is whose the size limited by Int.MaxValue? Thanks.


From the NumericRange docs,

NumericRange is a more generic version of the Range class which works with arbitrary types. It must be supplied with an Integral implementation of the range type.

Factories for likely types include Range.BigInt, Range.Long, and Range.BigDecimal. Range.Int exists for completeness, but the Int-based scala.Range should be more performant.

val r1 = new Range(0, 100, 1)
val veryBig = Int.MaxValue.toLong + 1
val r2 = Range.Long(veryBig, veryBig + 100, 1)
assert(r1 sameElements r2.map(_ - veryBig))


In my opinion the other answer is just wrong.

It demonstrates that you can use other number types, but this doesn't change the fact that a Range can only hold 2³¹ elements, like every other collection in Scala/Java.

As far as I know there is no real rationale behind this design decision. Having 64-bit collections would be certainly nice and support for arrays with 64bit indices are common for Java, but it is hard to integrate that into the existing language/collection framework. Some people say that the JVM is limited to a total of 4 billion objects, but I couldn't verify that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜