Scala – Empty Values

  • date 22nd May, 2021 |
  • by Prwatech |
  • 0 Comments

Dealing with Null Values in Scala

 

The Empty values in Scala are addressed by Null, null, Nil, Nothing, None, and Unit. The explanation of these vacant qualities are as per the following:

null: The reference types, for example, Objects, and Strings can be invalid and the worth kinds like Int, Double, Long, and so forth, can't be invalid, the invalid in Scala is practically equivalent to the invalid in Java.

Null: it is a Trait, which is a subset of every one of the reference types however isn't at all a sub-kind of significant worth sorts and a solitary occurrence of Null will be invalid. The reference types can be alloted invalid yet the worth kinds can't be allocated invalid.Dealing with Null Values in Scala

Use case 1:


object prwatech {
  // Main method
    def main(args: Array[String]) 
    {
        // Method that takes a parameter of type Null
        def usingnull(thing: Null): Unit = 
        { 
            println("Prwatech"); 
        }
  
        /*error: type mismatch;
  
        found   : java.lang.String("hey")
  
        required: Null*/
        //usingnull("hey")
  
        // passing null itself
        usingnull(null)
    }
}

output:

Prwatech

Here, method usingnull consists a parameter of type Null, here we can only pass two things. null itself or a reference of type Null. when a string is passed as argument it didn’t work and generated an error.

Nothing: Nothing is likewise a Trait, which has no occurrences. It is a subset of every one of the unmistakable sorts. The significant intention of this Trait is to supply a return type for the strategies which reliably tosses a special case i.e, not so much as a solitary time returns for the most part. It is additionally useful in giving a sort to Nil.

Unit: The Unit is Scala is similar to the void in Java, which is use as a return sort of a capacities that is utilize with a capacity when the expresse capacity doesn't brings anything back.

Use case 2:

object prwatech {
  // Main method
    def main(args: Array[String]) 
    {
        // Method return type is unit
        def printNumber(num: (Int) => Unit) = 
        {
  
            num(1); 
            num(2); 
            num(3);
        }
          
        printNumber(println)
    }
}
1
2
3

Here, method printNumber takes a parameter called num, which has a type of (Int) => Unit. This means that num is a method that contains a single parameter of type Int. method printNumber return type of Unit, which means num should not return a value.

Nil: Nil is Consider as a List which has zero components in it. The kind of Nil is List[Nothing] and as express over, that Nothing has no occurrences, we can have a List which is affirm to be devastate.

Use case 3:

object prwatech {
 // Main method
    def main(args: Array[String]) 
    {
  
        // Displays empty list
        println(Nil)
    }
}

Output:

List()

Thus, we can see that an empty list is returned.

Quick Support

image image