list - 如何修复模式匹配详尽警告?

标签 list scala pattern-matching scala-collections

一些Scala代码:

val list = List(Some("aaa"), Some("bbb"), None, ...)

list.filter(_!=None).map {
  case Some(x) => x + "!!!"
  // I don't want to handle `None` case since they are not possible
  // case None
}

当我运行它时,编译器提示:
<console>:9: warning: match may not be exhaustive.
It would fail on the following input: None
                  list.filter(_!=None).map {
                                   ^
res0: List[String] = List(aaa!!!, bbb!!!)

如何在不提供 case None 的情况下修复该警告线?

最佳答案

如果您正在使用 map之后 filter ,您可以使用 collect .

list collect { case Some(x) => x + "!!!" } 

关于list - 如何修复模式匹配详尽警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24751748/

相关文章:

scala - 通用 T 作为 Spark Dataset[T] 构造函数

scala - JodaTime:用第一,第二,第三等日期格式化日期

java - 我正在尝试使用正则表达式来匹配输入文件中的一行

java - 如何将从一个字符串中取出的特殊字符插入到另一个字符串中?

html - 如何制作这样的下拉菜单(里面有列表的 div)?

android - android中的数据绑定(bind)复杂列表

java 抛出异常 java.lang.IndexOutOfBoundsException :

python - 删除列表中 2 个重复的交换子列表

scala - Spark : unpersist RDDs for which I have lost the reference

python - 正则表达式 - 匹配最后出现的括号