2
0
mirror of https://github.com/chubin/cheat.sheets synced 2024-11-19 03:25:44 +00:00
cheat.sheets/sheets/_scala/Options

18 lines
490 B
Plaintext
Raw Normal View History

// Optional values
val s: Option[Int] = Some(22)
val n: Option[Int] = None
// Removing optional values from a collection
val listOfOptions: List[Option[Int]] = List(Some(1), None, Some(2), None, None, Some(3))
listOfOptions.flatten // returns List(1, 2, 3)
// Get the value out of an option
Some(5).get // returns 5
// Handling None
// BAD
None.get // This will throw java.util.NoSuchElementException
// GOOD
None.getOrElse(1) // This will handle the exception and return 1