Skip to content
Dev Ideas
Go back

Unified Type System in Scala

Edit page

Unified Type System in Scala

Unified Type System in Scala

In Scala, the Unified Type System means that there is a single root type from which every other type inherits. This root type is Any, making every value and object in Scala part of the same type hierarchy.

The Any class has two direct subclasses:

Scala Unified Type System

AnyVal

AnyVal is the parent class of all value types. These are non-nullable types that represent primitive values.

The predefined value types are:

AnyRef

AnyRef is the parent class of all reference types. Every user-defined class in Scala implicitly extends AnyRef.

On the JVM, scala.AnyRef corresponds to Java’s java.lang.Object.

Why does Scala separate AnyVal and AnyRef?

Since Scala runs on the Java Virtual Machine (JVM), it needs to distinguish between value types and reference types.

Unlike languages such as Java or C, Scala does not expose primitive types directly. Instead, these types are represented as subclasses of AnyVal, giving them a consistent place in the type hierarchy while still allowing the compiler to optimize them efficiently.

Because of this unified hierarchy:

The following diagram illustrates the relationship between the root type, value types, and reference types.

Advantages of the Unified Type System

The unified type system provides several benefits:

These methods are available because every type ultimately inherits from Any, where these common operations are defined.

Example

The following example demonstrates how the unified type system allows values of different types to coexist in the same collection.

// Scala Program to print common elements
// from 2 lists

object Geek {

    def main(args: Array[String]) {

        // Creating a list with a fixed data type
        val GfG: List[String] =
            List("Geeks", "for", "Geeks")

        // Creating a list containing
        // different data types using Any
        val myList: List[Any] = List(
            "Geeks",
            "for",
            "Geeks",
            1000,
            525
        )

        myList.foreach(value => {

            // contains() is a universal function
            if (GfG.contains(value)) {
                println(value)
            }
        })
    }
}

Output

Geeks
for
Geeks

Explanation

In this example:

The program iterates through myList and checks whether each element exists in GfG using the contains() method.

Since contains() is available on Scala collections and all values participate in the unified type system, values of different types can be processed consistently while preserving type safety.

This unified approach makes Scala expressive, robust, and particularly well suited for functional programming and large-scale data processing applications.


Edit page
Share this post:

Previous Post
Reflection in Golang
Next Post
Composition in Golang