Scala模式匹配在嵌套案例类中并非详尽无遗
发布时间:2020-12-16 18:17:14 所属栏目:安全 来源:网络整理
导读:我有一个案例类层次结构来编码一些请求和处理错误: sealed trait OpError sealed trait RequestErrorType sealed trait ProcessingErrorType final case class InvalidEndpoint(reason: String) extends RequestErrorType final case class InvalidParamete
我有一个案例类层次结构来编码一些请求和处理错误:
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 有任何想法吗? 解决方法
这是一个确认的错误.
I filed a bug report for this,自从Scala 2.12.0-M4修复以来.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |