我在SBT构建中设置了-Xfatal-warnings
编译器标志,确定这段代码不能编译:
package example
sealed trait Errors
object Errors {
case class BadFirstName(name: String) extends Errors
case class BadLastName(name: String) extends Errors
}
...
def handleError(errors: Errors): String = errors match {
case BadFirstName(name) => s"$name is bad"
}
我看到错误[error] It would fail on the following input: BadLastName(_)
但是如果我为BadFirstName
匹配添加一个守卫,它会编译而不会出现错误或警告:
def handleError(errors: Errors): String = errors match {
case BadFirstName(name) if name.startsWith("t") => s"$name is bad"
}
...
[info] Done compiling.
然后在运行时用可怕的[error] scala.MatchError: BadLastName(...)
这对我来说似乎是一个错误,但我想检查一下我是否错过了什么?