Option的解釋: Represents optional values. Instances of
Option
are either an instance of
scala.Some or the object
None
.
Option[A] (sealed trait) 有兩個取值:
1. Some[A] 有類型A的值
2. None 沒有值
Option一般有兩種用法:
1.
模式匹配
Option[A] option option match { case Some(a) => a case None => "?" }
2. map
option map( o => "?" ).getOrElse("默認值")
Some的解釋: Class
Some[A]
represents existing values of type
A
.
Some[A] some是一定有值的, some.get獲取值,如果沒有值, 會報異常.
Predef.NoSuchElementException if the option is empty.