有没有办法在Scala中抑制*仅*详尽检查?
我发现自己写了很多这样的代码:
thing match {
case Case1 ...
case Case2 ...
case _ => throw new IllegalStateException(s"unexpected $thing")
}
有时,当案例不匹配时,我想要一个运行时错误。案例是一种形式的断言。
有没有更好的方法来抑制穷举检查?
我不想使用,[@unchecked](https://www.scala-lang.org/api/2.12.1/scala/unchecked.html)因为这也会禁用可达性检查,这是我想要的。
回答
您可以使用nowarn注释来隐藏警告:
import scala.annotation.nowarn
@nowarn("msg=not.*?exhaustive")
val r = thing match {
case Case1 ...
case Case2 ...
}
它是在 Scala 2.13.2中添加的,因此如果您使用的是旧版本,则需要使用消音器插件。
或者,使用消音器插件,您可以设置基于全局正则表达式的 suppresion。