Skip to content

Commit d6faa07

Browse files
paulpdcsobral
authored andcommitted
Additional documentation for the Array type, wi...
Additional documentation for the Array type, with short code examples of construction, access and update. Also links to ArrayOps and WrappedArray in order to explain where the additional collections operations come from. Contributed by [email protected].
1 parent ce4e81a commit d6faa07

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

src/library/scala/Array.scala

+31-1
Original file line numberDiff line numberDiff line change
@@ -476,11 +476,41 @@ object Array extends FallbackArrayBuilding {
476476
fromFunction(i => fromFunction(f(i, _, _, _, _))(n2, n3, n4, n5))(n1)
477477
}
478478

479-
/** Represents polymorphic arrays. `Array[T]` is Scala's representation
479+
/** Arrays are mutable, indexed collections of values. `Array[T]` is Scala's representation
480480
* for Java's `T[]`.
481481
*
482+
* {{{
483+
* val numbers = Array(1, 2, 3, 4)
484+
* val first = numbers(0) // read the first element
485+
* numbers.update(3, 100) // replace the 4th array element with 100
486+
* val biggerNumbers = numbers.map(_ * 2) // multiply all numbers by two
487+
* }}}
488+
*
489+
* Two implicit conversions exist in [[scala.Predef]] that are frequently applied to arrays: a conversion
490+
* to [[scala.collection.mutable.ArrayOps]] and a conversion
491+
* to [[scala.collection.mutable.WrappedArray]] (a subtype of [[scala.collections.Seq]]).
492+
* Both types make available many of the standard operations found in the Scala collections API.
493+
* The conversion to `ArrayOps` is temporary, as all operations defined on `ArrayOps` return an `Array`,
494+
* while the conversion to `WrappedArray` is permanent as all operations return a `WrappedArray`.
495+
*
496+
* The conversion to `ArrayOps` takes priority over the conversion to `WrappedArray`. For instance,
497+
* consider the following code:
498+
*
499+
* {{{
500+
* val arr = Array(1, 2, 3)
501+
* val arrReversed = arr.reverse
502+
* val seqReversed : Seq[Int] = arr.reverse
503+
* }}}
504+
*
505+
* Value `arrReversed` will be of type `Array[Int]`, with an implicit conversion to `ArrayOps` occurring
506+
* to perform the `reverse` operation. The value of `seqReversed`, on the other hand, will be computed
507+
* by converting to `WrappedArray` first and invoking the variant of `reverse` that returns another
508+
* `WrappedArray`.
509+
*
482510
* @author Martin Odersky
483511
* @version 1.0
512+
* @see [[http://www.scala-lang.org/docu/files/collections-api/collections_38.html "The Scala 2.8 Collections API"]]
513+
* by Martin Odersky for more information.
484514
*/
485515
final class Array[T](_length: Int) extends java.io.Serializable with java.lang.Cloneable {
486516

0 commit comments

Comments
 (0)