Un-optioning an optioned Option

后端 未结 6 1215
甜味超标
甜味超标 2021-02-02 07:05

Say I have a val s: Option[Option[String]]. It can thus have the following values:

Some(Some(\"foo\")) Some(None) None

6条回答
  •  轮回少年
    2021-02-02 07:32

    Well, I actually don't understand how come it could be just None (the third case). If it can really be also just None, then I would vote for Rex Kerr's answer, otherwise just .get would be enough:

    scala> Some(Some("foo")).get
    res0: Some[java.lang.String] = Some(foo)
    
    scala> Some(None).get
    res1: None.type = None
    

提交回复
热议问题