Chapter 7. The Scala Object System

The Predef Object

For your convenience, whenever you compile code, the Scala compiler automatically imports the definitions in the java.lang package (javac does this, too). On the .NET platform, it imports the system package. The compiler also imports the definitions in the analogous Scala package, scala. Hence, common Java or .NET types can be used without explicitly importing them or fully qualifying them with the java.lang. prefix, in the Java case. Similarly, a number of common, Scala-specific types are made available without qualification, such as List. Where there are Java and Scala type names that overlap, like String, the Scala version is imported last, so it “wins.”

The compiler also automatically imports the Predef object, which defines or imports several useful types, objects, and functions.

Tip

You can learn a lot of Scala by viewing the source for Predef. It is available by clicking the “source” link in the Predef Scaladoc page, or you can download the full source code for Scala at http://www.scala-lang.org/.

Table 7-1 shows a partial list of the items imported or defined by Predef on the Java platform.

Table 7-1. Items imported or defined by Predef

Types

Character, Class, Error, Function, Integer, Map, Pair, Runnable, Set, String, Throwable, Triple.

Exceptions

Exception, ArrayIndexOutOfBoundsException, ClassCastException, IllegalArgumentException, IndexOutOfBoundsException, NoSuchElementException, NullPointerException, NumberFormatException ...

Get Programming Scala 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.