scala中Option和Some

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.
 
posted @ 2017-02-09 10:46  Entropy_lxl  阅读(20845)  评论(0编辑  收藏  举报