Unified Type System In Scala
Last Updated :
04 May, 2020
In this article we shall discuss how the
Unified Type System works in Scala. A Unified Type System essentially means that there is one
Super-Type from which other
Sub-Types inherit. In Scala the Super-Type is the class
Any. Therefore class Any is referred to as the
root. From Any, two subclasses are derived.
- AnyVal: All the value types extend to the AnyVal class. There are nine predefined value types and they are non-null able: Double, Float, Long, Int, Short, Byte, Char, Unit, and Boolean.
- AnyRef: All the reference types extend to the AnyRef class. User-defined classes define reference types by default; i.e. they always (indirectly) subclass scala.AnyRef. scala.AnyRef in java programming corresponds to java.lang.Object.
Since Scala runs on the Java Virtual Machine it needs to explicitly differentiate between Value types and Reference Types. For this purpose, the two subclasses AnyVal and AnyRef can be inherited from the root, i.e, Any. Unlike most other languages like Java, C etc. Scala does not have in-built primitive types. These byte sized data types are known as Value Types and are sub-classes of the AnyVal class which in turn extends to Any(root). This is similar to wrapper classes or boxed classes in languages such as Java. In other words, owing to the Unified Type System, all the byte sized data types are sub-classes, inherit from the Root Class. However, variables in Scala belonging to the Value Types cannot be instantiated with the new keyword. The new keyword can be used for Reference types.
The Following Diagram Represents how the valued and the reference types are related to the parent classes and the root.

In the above diagram,
- Arrows represent inheritance.
- Double lines represent the hierarchy and implicit type conversion.
Advantages of using a Unified Type System are as follows:
- A greater amount of Type Safety as opposed to other type systems.
- Interoperability and integration of objects created in other languages is easier.
- All variables have access to certain universal methods such as equals, hashCode, toString. This is because all the variables are instances of classes which are inherited from the Root class Any, where the universal methods are defined.
Let us see the functionality of having Unified Type System with an Example.
Scala
// 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 which can take
// variable data type input by Using Any
val myList : List[Any] = List(
"Geeks",
"for",
"Geeks",
1000,
525)
myList.foreach( value => {
//.contains() is an universally declared function
if (GfG.contains(value)){
println(value)
}
})
}
}
OUTPUT:
Geeks
for
Geeks
In the above example we see that we were able to create a list containing values of different data types by specifying the type
Any. Since Any is the root class, this expression is legal. Further, we have used a method .contains() to see if the List GfG contains a particular element. The .contains() method can be used on Lists, Sets and certain other collections since it is an universal function which is present in Any class. This is possible only due to the Unified Type System which thus makes scala more robust and functional as a language to deal with Big Data Analytics.
Similar Reads
Banking System using Scala In this project, we will build a simple Bank Management System using Scala which will perform the following task: 1) Show the existing details of all customers or a specific customer. 2) Add or remove an account 3) Deposit or Withdraw money 4) Checks the overdraft limit 5) Shows interest and dividen
14 min read
Scala Any type In Scala, the Any class is at the top of the hierarchy of classes. Scala stands like the supertype of all types and provides a foundation for Scala's type system. Any is like a supertype from which all values in scala are inherited whether it is a value type like(Int, Double, Float, etc.) or a refer
7 min read
Scala AnyRef type The Scala kind hierarchy starts with Any, that's the supertype of all types. The direct subclasses of Any are AnyVal and AnyRef. Whereas AnyVal represents price kinds (which includes Int, Double, and so on.), AnyRef represents reference types. AnyRef serves because the fundamental type and root deta
3 min read
How to import structtype in Scala? Scala stands for scalable language. It was developed in 2003 by Martin Odersky. It is an object-oriented language that provides support for a functional programming approach as well. Everything in scala is an object e.g. - values like 1,2 can invoke functions like toString(). Scala is a statically t
4 min read
Scala Tutorial â Learn Scala with Step By Step Guide Scala is a general-purpose programming language that combines both object-oriented and functional programming. Designed for scalability and flexibility, it allows developers to write concise and maintainable code for everything from small scripts to large enterprise systems. First released in June 2
15+ min read