Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A <code>Range</code> 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 <code>Array</code> on the other can hold arbitrary values, so its constructor must be told explicitly what those values are.</p> <p>The definition of <code>Range.apply</code> is that it takes either:</p> <ul> <li>two arguments: a start and end for a range, or </li> <li>three arguments: a start, end, and step size for the range.</li> </ul> <p>Here are the definitions of <code>apply</code> from <code>scala.collection.immutable.Range</code>:</p> <pre><code>/** 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) </code></pre> <p>Constrast this with the <code>apply</code> for <code>scala.Array</code>, which accepts a variable-length argument <code>T*</code>:</p> <pre><code>/** 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 &lt;- xs.iterator) { array(i) = x; i += 1 } array } </code></pre> <p>If your goal is to have an <code>Array</code> of the numbers 1 to 4, try this:</p> <pre><code>(1 to 4).toArray </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload