Option

The Option type is used frequently in Scala programs, and you can compare this with the null value available in Java, which indicates no value. Scala Option [T] is a container for zero or one element for a given type. An Option [T] can be either a Some [T] or None object, which represents a missing value. For instance, the get method of Scala's Map produces Some (value) if a value corresponding to a given key has been found, or None if the given key is not defined in the Map.

The basic trait for an Option looks like this:

trait Option[T] {  def get: A // Returns the option's value.  def isEmpty: Boolean // Returns true if the option is None, false  otherwise.  def productArity: Int // The size of this product. For a product A(x_1, ..., ...

Get Scala and Spark for Big Data Analytics now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.