O'Reilly logo

Programming Scala by Alex Payne, Dean Wampler

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The Scala Type Hierarchy

We have mentioned a number of types in Scala’s type hierarchy already. Let’s look at the general structure of the hierarchy, as illustrated in Figure 7-1.

Scala’s type hierarchy

Figure 7-1. Scala’s type hierarchy

Tables 7-2 and 7-3 discuss the types shown in Figure 7-1, as well as some other important types that aren’t shown. Some details are omitted for clarity. When the underlying “runtime” is discussed, the points made apply equally to the JVM and the .NET CLR, except where noted.

Table 7-2. Any, AnyVal, and AnyRef

NameParentDescription

Any

none

The root of the hierarchy. Defines a few final methods like ==, !=, isInstanceOf[T] (for type checking), and asInstanceOf[T] (for type casting), as well as default versions of equals, hashCode, and toString, which are designed to be overridden by subclasses.

AnyVal

Any

The parent of all value types, which correspond to the primitive types on the runtime platform, plus Unit. All the AnyVal instances are immutable value instances, and all the AnyVal types are abstract final. Hence, none of them can be instantiated with new. Rather, new instances are created with literal values (e.g., 3.14 for a Double) or by calling methods on instances that return new values.

AnyRef

Any

The parent of all reference types, including all java.* and scala.* types. It is equivalent to java.lang.Object for the JVM and object (System.Object) for the .NET runtime. Instances ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required