println("Step 1: How to use Option and Some - a basic example")val glazedDonutTaste: Option[String] = Some("Very Tasty") println(s"Glazed Donut taste = ${glazedDonutTaste.get}")
println("\nStep 2: How to use Option and None - a basic example")val glazedDonutName: Option[String] = Noneprintln(s"Glazed Donut name = ${glazedDonutName.getOrElse("Glazed Donut")}")
println("\nStep 3: How to use Pattern Matching with Option")glazedDonutName match {case Some(name) => println(s"Received donut name = $name")case None => println(s"No donut name was found!")
这样我们不管Option里面到底有没有值,都可以完成匹配。下面是输出的结果。
Step 3: How to use Pattern Matching with OptionNo donut name was found!