Open In App

Unified Type System In Scala

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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.

  1. 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.
  2. 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 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.



Last Updated : 04 May, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads