Introduction

Scala’s packaging approach is similar to Java, but it’s more flexible. In addition to using the package statement at the top of a class file, you can use a curly brace packaging style, similar to C++ and C# namespaces.

The Scala approach to importing members is also similar to Java, and more flexible. With Scala you can:

  • Place import statements anywhere

  • Import classes, packages, or objects

  • Hide and rename members when you import them

All of these approaches are demonstrated in this chapter.

It’s helpful to know that in Scala, two packages are implicitly imported for you:

  • java.lang._

  • scala._

In Scala, the _ character is similar to the * character in Java, so these statements refer to every member in those packages.

In addition to those packages, all members from the scala.Predef object are imported into your applications implicitly.

A great suggestion from the book Beginning Scala by David Pollak (Apress), is to dig into the source code of the Predef object. The code isn’t too long, and it demonstrates many of the features of the Scala language. Many implicit conversions are brought into scope by the Predef object, as well as methods like println, readLine, assert, and require.

Get Scala Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.