Skip to content

Array.range throw ArrayIndexOutOfBoundsException if overflow. not consists with other collections #24877

@xuwei-k

Description

@xuwei-k

Compiler version

  • 2.13.18
  • 3.3.7
  • 3.7.4
  • 3.8.0-RC5

Minimized code

//> using scala 3.8.0-RC5

object Main {
  def main(args: Array[String]): Unit = {
    println(List.range(2, 5, Int.MaxValue))
    println(Vector.range(2, 5, Int.MaxValue))
    println(Array.range(2, 5, Int.MaxValue))
  }
}

Output

List(2)
Vector(2)
[error] java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
[error] 	at scala.Array$.range(Array.scala:522)
[error] 	at Main$.main(Main.scala:7)
[error] 	at Main.main(Main.scala)

Expectation

same as other collection

Note

  • /** Returns an array containing equally spaced values in some integer interval.
    *
    * @param start the start value of the array
    * @param end the end value of the array, exclusive (in other words, this is the first value '''not''' returned)
    * @param step the increment value of the array (may not be zero)
    * @return the array with values in `start, start + step, ...` up to, but excluding `end`
    */
    def range(start: Int, end: Int, step: Int): Array[Int] = {
    if (step == 0) throw new IllegalArgumentException("zero step")
    val array = new Array[Int](immutable.Range.count(start, end, step, isInclusive = false))
    var n = 0
    var i = start
    while (if (step < 0) end < i else i < end) {
    array(n) = i
    i += step
    n += 1
    }
    array
    }

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions