Skip to content

using take and drop on wide NumericRanges causes IllegalArgumentException #12706

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
AminMal opened this issue Dec 30, 2022 · 1 comment · Fixed by scala/scala#10259
Closed

Comments

@AminMal
Copy link

AminMal commented Dec 30, 2022

Reproduction steps

Given a wide NumericRange, say BigInt(0) until (BigInt(Int.MaxValue) + 1000), if you try to take some amount from the range, or drop some from it, it causes IllegalArgumentException. question on stackoverflow

Scala version: 3.x, 2.13.x

scala> val range = BigInt(0) until (BigInt(Int.MaxValue) + 1000)
val range: scala.collection.immutable.NumericRange.Exclusive[scala.math.BigInt] = NumericRange 0 until 2147484647

scala> range.take(2)
java.lang.IllegalArgumentException: More than Int.MaxValue elements.
  at scala.collection.immutable.NumericRange$.check$1(NumericRange.scala:324)
  at scala.collection.immutable.NumericRange$.count(NumericRange.scala:334)
  at scala.collection.immutable.NumericRange.length$lzycompute(NumericRange.scala:75)
  at scala.collection.immutable.NumericRange.length(NumericRange.scala:75)
  at scala.collection.immutable.NumericRange.take(NumericRange.scala:143)
  ... 32 elided

scala> range.drop(Int.MaxValue)
java.lang.IllegalArgumentException: More than Int.MaxValue elements.
  at scala.collection.immutable.NumericRange$.check$1(NumericRange.scala:324)
  at scala.collection.immutable.NumericRange$.count(NumericRange.scala:334)
  at scala.collection.immutable.NumericRange.length$lzycompute(NumericRange.scala:75)
  at scala.collection.immutable.NumericRange.length(NumericRange.scala:75)
  at scala.collection.immutable.NumericRange.drop(NumericRange.scala:149)
  ... 32 elided

Problem

The problem is the unexpected behavior of the range. It's the fact that you are able to create a wide range, but you can not take a small piece of it.

@retronym
Copy link
Member

I've logged a related issue with NumericRangeIterator #13100

@AminMal does this look fixable by following the same approach you took?

hamzaremmal pushed a commit to hamzaremmal/scala3 that referenced this issue May 2, 2025
hamzaremmal pushed a commit to scala/scala3 that referenced this issue May 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants