取消选择选项 [英] Un-optioning an optioned Option

查看:170
本文介绍了取消选择选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个val s: Option[Option[String]].因此,它可以具有以下值:

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

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

我想减少它,以便第一个变成Some("foo"),而另两个变成None.显然,有很多方法可以完成此操作,但是我正在寻找一种简单的,也许内置的,少于一个内衬的纸.

I want to reduce it so that the first becomes Some("foo") while the two others become None. Obviously there are many ways to accomplish this, but I'm looking for a simple, perhaps built-in, less-than-one-liner.

推荐答案

flatten不存在是一个遗憾.应该.

It's a shame that flatten doesn't exist. It should.

现在平底版确实存在.

和以前一样,

s getOrElse None

(除了其他答案)也将做同样的事情.

(in addition to the other answers) will also do the same thing.

这篇关于取消选择选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆