处理 Scala 选项[T]
我有一个 Scala Option[T]
。如果值为 Some(x)
我想使用不返回值 (Unit
) 的进程来处理它,但如果它为 None,我想打印一个错误。
我可以使用以下代码来执行此操作,但我知道更惯用的方法是将 Option[T]
视为序列并使用 map
, foreach
等。我该怎么做?
opt match {
case Some(x) => // process x with no return value, e.g. write x to a file
case None => // print error message
}
I have a Scala Option[T]
. If the value is Some(x)
I want to process it with a a process that does not return a value (Unit
), but if it is None
, I want to print an error.
I can use the following code to do this, but I understand that the more idiomatic way is to treat the Option[T]
as a sequence and use map
, foreach
, etc. How do I do this?
opt match {
case Some(x) => // process x with no return value, e.g. write x to a file
case None => // print error message
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为显式模式匹配最适合您的用例。
I think explicit pattern matching suits your use case best.
遗憾的是,Scala 的
Option
缺少执行此操作的方法。我添加一个:它的用法稍微好一点(在我看来)。
从它的定义方式可以看出,可以使用
map
/getOrElse
来代替模式匹配。或者,
Either
已经有一个fold
方法。所以你可以,但这有点笨拙,因为你必须提供左值(这里
()
,即单位),然后在其上定义一个函数,而不是仅仅说明你想要发生的事情在无
上。模式匹配也不错,特别是对于较长的代码块。对于短距离比赛来说,比赛的开销开始阻碍得分。例如:
语法开销比
and 因此,一旦你期望它,就更容易理解。
Scala's
Option
is, sadly, missing a method to do exactly this. I add one:which has the slightly nicer (in my view) usage
You can see from how it's defined that
map
/getOrElse
can be used instead of pattern matching.Alternatively,
Either
already has afold
method. So you canbut this is a little clumsy given that you have to provide the left value (here
()
, i.e. Unit) and then define a function on that, rather than just stating what you want to happen onNone
.The pattern match isn't bad either, especially for longer blocks of code. For short ones, the overhead of the match starts getting in the way of the point. For example:
has a lot less syntactic overhead than
and therefore, once you expect it, is a lot easier to comprehend.
我建议简单而安全地使用
opt.get
,如果opt
为None
,它本身会抛出NoSuchElementException
异常。或者如果你想抛出自己的异常,你可以这样做:I'd recommend to simply and safely use
opt.get
which itself throws aNoSuchElementException
exception ifopt
isNone
. Or if you want to throw your own exception, you can do this:正如@missingfaktor所说,您正处于模式匹配给出最可读结果的确切场景中。
如果 Option 有一个值,你想做某事,如果没有,你想做别的事。
虽然有多种方法可以在选项类型上使用映射和其他函数构造,但它们通常在以下情况下很有用:
您想要使用 Some 情况并忽略 None 情况,例如在您的情况下
,或者您想要对多个选项进行链接操作并且只有当它们全部都是 Some 时才给出结果。例如,这样做的一种方法是:
但这些似乎都不是你的情况
as @missingfaktor says, you are in the exact scenario where pattern matching is giving the most readable results.
If Option has a value you want to do something, if not you want to do something else.
While there are various ways to use map and other functional constructs on Option types, they are generally useful when:
you want to use the Some case and ignore the None case e.g. in your case
or you want to chain the operations on more than one option and give a result only when all of them are Some. For instance, one way of doing this is:
but none of these seem to be your case
模式匹配是这里的最佳选择。
但是,如果您想将 Option 视为序列并映射它,您可以这样做,因为 Unit 是一个值:
顺便说一句,打印错误是某种“反模式”,因此最好抛出无论如何,一个例外:
Pattern matching is the best choice here.
However, if you want to treat Option as a sequence and to map over it, you can do it, because Unit is a value:
By the way, printing an error is some kind of "anti-pattern", so it's better to throw an exception anyway: