从Scala移除警告

时间:2019-06-03 07:24:39

标签: scala

我有一个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 {

请建议如何删除此警告。

2 个答案:

答案 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
      }