Sage-Code Laboratory
index<--

Scala Packages

Scala uses packages to create namespaces which allow you to modularize your project. Some packages are specific to a particular project. Other packages can have general functionality that can be reused in more than one project.

Create a Package

Packages are created by declaring one package name at the top of a Scala source code file. There is one convention to name the package the same as the directory containing the Scala file. Many files in one folder can be part of the same package.

//simple package
package package_name
// Traits
...
// Classes
...
// Objects
...

Block Package

You can define a block package by using curly brackets after the package name to create a package scope. This can encapsulate traits, classes, objects known as package members.Let's see an example:

//simple block package
package users {
  //members of users package
  ...
}

Multiple Packages

Using multiple packages/file you can create multiple "sections" of code each having a role of container. You can use all packages defined in one file later and you do not have to import them. But, you need to use dot notation to access some other packages previously defined.

// define a block package
package orderentry {
  class Foo { 
     override def toString = "orderentry.Foo" 
  }
}

// one package nested inside the other
package customers {
  class Foo { 
     override def toString = "customers.Foo" 
  }
  package database {
    // this Foo is different than customers.Foo or orderentry.Foo
    class Foo { override def toString = "customers.database.Foo" }
  }
}

object PackageTests extends App {
  println(new orderentry.Foo)
  println(new customers.Foo)
  println(new customers.database.Foo)
}

Package Names

Like Java, we use same convention in Scala to create packages in a specific folder and give package specific names related to a domain name (inverted). For example if Sage-Code will create a Scala package named: Quiz it will have the following name:

//domain specific package name
package net.sagecode.quiz

class test 
...

This file could be located in a folder path as follows:

/scala/quiz/src/main/net/sagecode/quiz/test.scala

Import Packages

To use a package from other package you must "import" the package. Of course you can import one or more packages. Import clauses are selective. Let's see some examples:

//import one class
import java.io.File

// import every class in a package
import java.io._

In Java there is also an "import" statement that can be used in top of the source file. In Scala, you can use import on top of your source file but also anywhere it's neaded. For example next method need to import a library before computing anything:


def arctan(x: Double):Double = {
    import scala.math.sqrt
    1/tan(x)
}

Specify Members

When you import, you can specify exactly what classes you need to use. These classes become known in your package and you can use them as if they are defined in your package.

/* import multiple classes from a package */

// Sollution A: using curly brackets
import java.io.{File, IOException, FileNotFoundException}

// Sollution B: specify one class/line
import java.io.File
import java.io.FileNotFoundException
import java.io.IOException

Member Aliasing

You can give a class a new name/alias when you import it by using "=>" symbol:

//import with alias demo
import java.util.{List => UtilList}
import java.awt.{List => AwtList}

// later ...
val list = new AwtList

Hiding Classes

You can hide one or more classes while importing. The following example hides the Random class, while importing everything else from the java.util package:

//you can use _ alias to hide classes
import java.util.{Random => _, _}

Default Packages

The "scala" and "java.lang" packages as well as "object Predef" are imported by default in any Scala project. What is in these packages do not require any import to be made, therefore many of our examples do not import anything.

Standard Packages

Scala comes with many packages available when you install Scala on your computer. These packages are part of Scala "standard library" and you can use it any time you need it for free becouse Scala is an open source language.

package name Description
scala.collection Scala's collections framework
scala.concurrent Primitives for concurrent programming
scala.io Input and output operations
scala.math Basic math functions and numeric types
scala.sys Interaction with other platforms
scala.util.matching Regular expressions

See also: scala root package


Go back to: Scala Tutorial