This article is a collection of Scala packaging and import examples. I created most of these examples when I was writing the 1st Edition of the Scala Cookbook. I share them here without much discussion, but for more examples and discussion, please check out the Cookbook.
Packages imported by default
By default, three packages are implicitly imported for you:
- java.lang._
- scala._
- scala.Predef._
Basic Scala import usage
Here are some import
examples:
// import one class import java.io.File // import every class in a package import java.io._ // import multiple classes from a package (version 1) import java.io.{File, IOException, FileNotFoundException} // import multiple classes from a package (version 2) import java.io.File import java.io.FileNotFoundException import java.io.IOException
Creating a class alias during import (renaming a class)
You can give a class a new name (or alias) when you import it:
import java.util.{List => UtilList} import java.awt.{List => AwtList} // later ... val list = new AwtList
Hiding classes during import
You can hide one or more classes while importing others. The following example hides the Random
class, while importing everything else from the java.util package:
import java.util.{Random => _, _}
This is confirmed in the REPL:
scala> import java.util.{Random => _, _} import java.util.{Random=>_, _} // can't access Random scala> val r = new Random <console>:10: error: not found: type Random val r = new Random ^ // can access other members scala> new ArrayList res0: java.util.ArrayList[Nothing] = []
The equivalent of Java’s static import
You can import members in a way similar to the Java static import approach:
import java.lang.Math._ // demonstrated in the repl scala> import java.lang.Math._ import java.lang.Math._ scala> val a = sin(0) a: Double = 0.0 scala> val a = cos(PI) a: Double = -1.0
Basic packaging
You can create Scala packages with the usual Java practice of declaring a package name at the top of the file:
package foo.bar.baz class Foo { override def toString = "I'm foo.bar.baz.Foo" }
Curly brace style packaging
You can also use a “curly braces” style:
// a single package package orderentry { class Foo { override def toString = "I am orderentry.Foo" } } // one package nested inside the other package customers { class Foo { override def toString = "I am customers.Foo" } package database { // this Foo is different than customers.Foo or orderentry.Foo class Foo { override def toString = "I am customers.database.Foo" } } } object PackageTests extends App { println(new orderentry.Foo) println(new customers.Foo) println(new customers.database.Foo) }
Nesting packages
You can easily nest packages:
package foo { package bar { package baz { class Foo { override def toString = "I'm foo.bar.baz.Foo" } } } }
You can define multiple levels of depth at one time:
package com.alvinalexander.foo { class Foo { override def toString = "I am com.alvinalexander.foo.Foo" } }
Summary
As I mentioned, these are some of the examples you’ll find in the Scala Cookbook. Please see the Cookbook for more examples and details. You can find it at these links: