我有一个案例类层次结构来编码一些请求和处理错误:
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
有任何想法吗?
解决方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。