我有一个scala代码。
val lines = Source
.fromResource("doc-topics-new.txt")
.getLines
.toList
.drop(1) match {
case x :: xs => x.split(" ").drop(2).mkString(" ") :: xs
}
当我运行代码时,它一直在工作,但是始终会有警告消息
Warning:(81, 14) match may not be exhaustive.
It would fail on the following input: Nil
.drop(1) match {
请建议如何删除此警告。
答案 0 :(得分:4)
只需添加一个Nil
的情况:
val lines = Source
.fromResource("doc-topics-new.txt")
.getLines
.toList
.drop(1) match {
case Nil => List.empty // Add this line
case x :: xs => x.split(" ").drop(2).mkString(" ") :: xs
}
答案 1 :(得分:-1)
您只需将Nil
大小写添加到模式匹配中即可。
如果您确实想抑制该警告,则可以使用unchecked批注:
val lines = (Source
.fromResource("doc-topics-new.txt")
.getLines
.toList
.drop(1): @unchecked) match {
case x :: xs => x.split(" ").drop(2).mkString(" ") :: xs
}