1.

Explain Option and write its usage.

Answer»

The Scala Option[T] is referred to as a carrier of one element or none for a specified type. As the name suggests, this container holds either a Some or a None object, which represents a missing value. It holds Some[T] if a value is stored, otherwise none. In simple words, Scala options are WRAPPERS for missing values. 

EXAMPLE:

 object option { def main(args: Array[String]) { val employee: Map("InterviewBit" -> "Aarav", "Scaler" -> "ANKESH") val a= employee.get("InterviewBit") val B= employee.get("Informatica") println(a); println(b); } }

Output:  

Some(Aarav) None

Here, the key of the value InterviewBit is FOUND, therefore, Some is returned, however, the key of the value Informatica is not found, therefore, None is returned. 



Discussion

No Comment Found