This page is no longer maintained — Please continue to the home page at

A Tour of Scala: Packages

A package is a special object which defines a set of member classes, objects and packages. Unlike other objects, packages are not introduced by a definition.

A packaging package p { ds } injects all definitions in ds as members into the package whose qualified name is p. Members of a package are called top-level definitions. If a definition in ds is labeled private, it is visible only for other members in the package.

A protected modifier can be qualified with an package identifier p (e.g. protected[p]). Members labeled with such a modifier are also accessible from all code inside the package p.

Selections p.m from p as well as imports from p work as for objects. However, unlike other objects, packages may not be used as values. It is illegal to have a package with the same fully qualified name as a module or a class.

Top-level definitions outside a packaging are assumed to be injected into a special empty package. That package cannot be named and therefore cannot be imported. However, members of the empty package are visible to each other without qualification.

A compilation unit package p; stats starting with a package clause is equivalent to a compilation unit consisting of a single packaging package p { stats }.

Several packages may be declared in the same Scala source file:

  package p1 {
    object test extends Application {
  package p2 {
    object test extends Application {

Import clauses

An import clause has the form import p.I where the import expression I determines a set of names of members of p which are made available without qualification. For example:

The clause makes available without qualification..
import p._ all members of p (this is analogous to import p.* in Java).
import p.x the member x of p.
import p.{x => a} the member x of p renamed as a.
import p.{x, y} the members x and y of p.
import p1.p2.z the member z of p2, itself member of p1.


Futhermore the clause import p1._, p2._ is a shorthand for import p1._; import p2._.

Implicitly imported into every compilation unit are, in that order:

  • the package java.lang,
  • the package scala,
  • and the object scala.Predef.

Members of a later import in that order hide members of an earlier import. 

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland