Scala - Type Hierarchy



Scala is a programming language that checks types at compile time. It supports both object-oriented and functional programming styles. A key part of Scala is its type system, which is used to define and use different types of data. To become good at Scala, you need to understand this type system. Because it is the foundation for many of the language advanced features. In this post, we will discuss type hierarchy in Scala.

The Root of All Types: Any

At the very top of the Scala type hierarchy is the Any type. It is the supertype of all types in Scala. Every type in Scala, whether it is a primitive type, user-defined class, or function, inherits from Any. Any the most general type. Any has two important subtypes: AnyVal and AnyRef.

The Root of All Types

Example

val anyValue: Any = "Hello, Scala!"

Value Types: AnyVal

AnyVal is the supertype of all value types. These are the types that represent primitive values. These are generally mapped to Java primitive types for performance reasons. Scala defines various value types, i.e., Int, Double, Boolean, Char, Unit, and others. These are important value types in Scala −

  • Int − It represents 32-bit integer values.
  • Double − It represents 64-bit floating point values.
  • Boolean − It represents true or false.
  • Char − It represents a 16-bit Unicode character.
  • Unit − It represents no value, similar to void in Java.

For example,

val intValue: Int = 42 val doubleValue: Double = 3.14 val booleanValue: Boolean = true

Reference Types: AnyRef

AnyRef is the supertype of all reference types. These are analogous to Java Object class. Every user-defined class, object and array in Scala inherits from AnyRef. AnyRef corresponds to Java Object and provides methods like equals, hashCode and toString.

For example,

class Person(val name: String, val age: Int) val person: AnyRef = new Person("Alice", 30)

In Scala, String is also a reference type and inherits from AnyRef.

Bottom Types: Nothing and Null

There are two special types at the bottom of the Scala type hierarchy: Nothing and Null.

Nothing is a subtype of every other type. There are no instances of Nothing. It is used to signal abnormal termination (like throwing an exception). It returns type for methods that never return normally.

For example,

def fail(message: String): Nothing = throw new RuntimeException(message)

Null is a subtype of all reference types (AnyRef), but not of value types (AnyVal). It represents the absence of a value. It is similar to null in Java. However, its usage is generally discouraged in favor of safer alternatives.

For example,

val nullValue: String = null

Special Types: Unit and Option

Unit is a value type. It signifies the absence of a meaningful value and has only one instance, (). It is similar to void in Java but is a first-class value in Scala.

For example,

def printMessage(message: String): Unit = { println(message) }

Option is not part of the type hierarchy. But it is an important type used to represent optional values. An Option can either be Some(value) if there is a value, or None if there is no value. This is a safer alternative to using null.

For example,

val someValue: Option[Int] = Some(42) val noValue: Option[Int] = None
Advertisements