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 theRange
class which works with arbitrary types. It must be supplied with anIntegral
implementation of the range type.Factories for likely types include
Range.BigInt
,Range.Long
, andRange.BigDecimal
.Range.Int
exists for completeness, but theInt
-basedscala.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.
精彩评论