Un-optioning an optioned Option

后端 未结 6 1230
甜味超标
甜味超标 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:28

    I think the conversion to the Iterable is just fine. Use these steps to go from Option[Option[String] to a single Option[String]

    s.flatten.headOption 
    

    (which returns Option[String])

提交回复
热议问题