Tutorial by Examples

Options have some useful higher-order functions that can be easily understood by viewing options as collections with zero or one items - where None behaves like the empty collection, and Some(x) behaves like a collection with a single item, x. val option: Option[String] = ??? option.map(_.trim) ...
In Java (and other languages), using null is a common way of indicating that there is no value attached to a reference variable. In Scala, using Option is preferred over using null. Option wraps values that might be null. None is a subclass of Option wrapping a null reference. Some is a subclass of...
An Option is a data structure that contains either a single value, or no value at all. An Option can be thought of as collections of zero or one elements. Option is an abstract class with two children: Some and None. Some contains a single value, and None contains no value. Option is useful in ex...
Options have a flatMap method. This means they can be used in a for comprehension. In this way we can lift regular functions to work on Options without having to redefine them. val firstOption: Option[Int] = Option(1) val secondOption: Option[Int] = Option(2) val myResult = for { firstValue ...

Page 1 of 1