Return a missing-safe result from a lookup.

Safe Lookup

SafeLookup.scala
object Main {
  def main(args: Array[String]): Unit = {
    val key = 
    var found: Option[Int] = None
    if (key == "a") {
      found = Some(10)
    }
    if (key == "b") {
      found = Some(20)
    }

    val status = if (found.isDefined) "hit" else "miss"
    val result = found.getOrElse(-1)

    println("key=" + key)
    println("status=" + status)
    println("result=" + result)
  }
}
object Main {
  def main(args: Array[String]): Unit = {
    val key = 
    var found: Option[Int] = None
    if (key == "a") {
      found = Some(10)
    }
    if (key == "b") {
      found = Some(20)
    }

    val status = if (found.isDefined) "hit" else "miss"
    val result = found.getOrElse(-1)

    println("key=" + key)
    println("status=" + status)
    println("result=" + result)
  }
}
object Main {
  def main(args: Array[String]): Unit = {
    val key = 
    var found: Option[Int] = None
    if (key == "a") {
      found = Some(10)
    }
    if (key == "b") {
      found = Some(20)
    }

    val status = if (found.isDefined) "hit" else "miss"
    val result = found.getOrElse(-1)

    println("key=" + key)
    println("status=" + status)
    println("result=" + result)
  }
}
safe-lookup A lookup can return an `Option` instead of a raw value. The caller turns it into a status label and a safe default with `isDefined` and `getOrElse`.