Restrictions and things to comeTopWhere to put package objectsThe Scala package objectContents

The Scala package object

The standard Scala package also has its package object. Because scala._ is automatically imported into every Scala file, the definitions of this object are available without prefix.

Here are the most important definitions in this package object. As you can see, the main purpose of this object is to make a number of often-used definitions nested in subpackages available from the scala package. In this way, we distinguish convenience of access from logical structure. For instance, the List type is used so often that it makes sense to put it in the scala package thereby making it accessible without an import or name qualification. On the other hand, List is an immutable collection class, so it belongs logically in package scala.collection.immutable. Using package objects, one can have both a logical structure and fast access.

package object scala { 
 
  // Type and value aliases for collection classes
 
  type TraversableOnce[+A] = scala.collection.TraversableOnce[A] 
 
  type Traversable[+A] = scala.collection.Traversable[A]
  val Traversable = scala.collection.Traversable
 
  type Iterable[+A] = scala.collection.Iterable[A]
  val Iterable = scala.collection.Iterable
 
  type Seq[+A] = scala.collection.Seq[A]
  val Seq = scala.collection.Seq
 
  type IndexedSeq[+A] = scala.collection.IndexedSeq[A]
  val IndexedSeq = scala.collection.IndexedSeq
 
  type Iterator[+A] = scala.collection.Iterator[A]
  val Iterator = scala.collection.Iterator
 
  type BufferedIterator[+A] = scala.collection.BufferedIterator[A]
 
  type List[+A] = scala.collection.immutable.List[A]
  val List = scala.collection.immutable.List
 
  val Nil = scala.collection.immutable.Nil
  
  type ::[A] = scala.collection.immutable.::[A]
  val :: = scala.collection.immutable.::
 
  type Stream[+A] = scala.collection.immutable.Stream[A]
  val Stream = scala.collection.immutable.Stream
  val #:: = scala.collection.immutable.Stream.#::
 
  type Vector[+A] = scala.collection.immutable.Vector[A]
  val Vector = scala.collection.immutable.Vector
   
  type StringBuilder = scala.collection.mutable.StringBuilder
 
  type Range = scala.collection.immutable.Range
  val Range = scala.collection.immutable.Range
 
  // Numeric types which were moved into scala.math.*
   
  type BigDecimal = scala.math.BigDecimal
  val BigDecimal = scala.math.BigDecimal
   
  type BigInt = scala.math.BigInt
  val BigInt = scala.math.BigInt
   
  type Equiv[T] = scala.math.Equiv[T]
  type Fractional[T] = scala.math.Fractional[T]
  type Integral[T] = scala.math.Integral[T]
 
  type Numeric[T] = scala.math.Numeric[T]
  val Numeric = scala.math.Numeric
   
  type Ordered[T] = scala.math.Ordered[T]
  val Ordered = scala.math.Ordered
   
  type Ordering[T] = scala.math.Ordering[T]
  val Ordering = scala.math.Ordering
   
  type PartialOrdering[T] = scala.math.PartialOrdering[T]    
  type PartiallyOrdered[T] = scala.math.PartiallyOrdered[T]
  ...
}

Note also that along with most of the type aliases in package scala comes a value alias of the same name. For instance, there's a type alias for the List class and a value alias for the List object. That way, one can not only access the type List without a prefix but also create list values with syntax such as List(1, 2, 3). If you decompose the latter expression you get

List.apply(123)

i.e.a call to the apply method of the scala.List value. That value in turn is an alias of the scala.collection.immutable.List object, which defines the apply method in question.

Next: Restrictions and things to come


Restrictions and things to comeTopWhere to put package objectsThe Scala package objectContents