我有一个案例类层次结构来编码一些请求和处理错误:
sealed trait OpError sealed trait RequestErrorType sealed trait ProcessingErrorType final case class InvalidEndpoint(reason: String) extends RequestErrorType final case class InvalidParameters(reason: String) extends RequestErrorType final case class InvalidFormat(response: String) extends ProcessingErrorType final case class EntityNotFound(id: Long) extends ProcessingErrorType final case class RequestError(errorType: RequestErrorType) extends OpError final case class ProcessingError(errorType: ProcessingErrorType) extends OpError
如果我在所有模式中编写简单匹配:
def printMatches(error: OpError): Unit = error match { case RequestError(InvalidEndpoint(reason)) => //print something case RequestError(InvalidParameters(reason)) => //print something case ProcessingError(InvalidFormat(format)) => //print something case ProcessingError(EntityNotFound(entityId)) => //print something }
编译器给我一个关于缺少匹配的警告:
match may not be exhaustive. It would fail on the following input: ProcessingError(_) def printMatches(error: OpError): Unit = error match {
但ProcessingError接收的ProcessingErrorType只有两个扩展名:InvalidFormat和EntityNotFound,两者都在模式匹配中考虑.我错过了什么?
更奇怪的是,如果我将InvalidParameters或InvalidEndpoint的参数类型更改为String*,我不会收到错误:
final case class InvalidParameters(reason: String*) extends RequestErrorType
有任何想法吗?