Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does val range = Range(1, 2, 3, 4) give error?

for (i <- Array.apply(1 to 4))
      print(i);

Range(1, 2, 3, 4)

Range(1, 10)

//res0: scala.collection.immutable.Range = Range(1, 2, 3, 4, 5, 6, 7, 8, 9)

So why does val range = Range(1, 2, 3, 4) give error?


1 Answers

A Range is a special kind of collection that is restricted in what it can represent in order to efficiently perform its operations. It is only able to represent a sequence of numbers with a fixed step in between elements. As such, it only needs to be told about the start, end, and step size in order to be constructed. An Array on the other can hold arbitrary values, so its constructor must be told explicitly what those values are.

The definition of Range.apply is that it takes either:

  • two arguments: a start and end for a range, or
  • three arguments: a start, end, and step size for the range.

Here are the definitions of apply from scala.collection.immutable.Range:

/** Make a range from `start` until `end` (exclusive) with given step value.
 * @note step != 0
 */
def apply(start: Int, end: Int, step: Int): Range = new Range(start, end, step)

/** Make an range from `start` to `end` inclusive with step value 1.
 */
def apply(start: Int, end: Int): Range = new Range(start, end, 1) 

Constrast this with the apply for scala.Array, which accepts a variable-length argument T*:

/** Creates an array with given elements.
 *
 *  @param xs the elements to put in the array
 *  @return an array containing all elements from xs.
 */
def apply[T: ClassManifest](xs: T*): Array[T] = {
  val array = new Array[T](xs.length)
  var i = 0
  for (x <- xs.iterator) { array(i) = x; i += 1 }
  array
}

If your goal is to have an Array of the numbers 1 to 4, try this:

(1 to 4).toArray
like image 103
dhg Avatar answered Mar 17 '26 03:03

dhg