Scala模式匹配在嵌套案例类中并非详尽无遗

ssa*_*anj 6 scala pattern-matching

我有一个案例类层次结构来编码一些请求和处理错误:

  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
Run Code Online (Sandbox Code Playgroud)

如果我在所有模式中编写简单匹配:

  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
  }
Run Code Online (Sandbox Code Playgroud)

编译器给我一个关于缺少匹配的警告:

 match may not be exhaustive.
 It would fail on the following input: ProcessingError(_)
 def printMatches(error: OpError): Unit = error match {
Run Code Online (Sandbox Code Playgroud)

但ProcessingError接收的ProcessingErrorType只有两个扩展名:InvalidFormat和EntityNotFound,两者都在模式匹配中考虑.我错过了什么?

更奇怪的是,如果我将InvalidParameters或InvalidEndpoint的参数类型更改为String*,我不会收到错误:

final case class InvalidParameters(reason: String*) extends RequestErrorType
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

ssa*_*anj 5

这是一个已确认的错误。我为此提交了错误报告,此后已针对 Scala 2.12.0-M4 进行了修复。