我们有一些模式匹配代码,我们期望编译器警告匹配并非详尽无遗,但我们得不到.是否存在无法进行详尽检查的情况?
例如我们的例子(使用scalactic Good + Bad):
(maybeModelIdOrFailure, maybeMake) match { case (Some(Good(modelId)), Some(makeId)) if modelId >= 0 && taxonomyService.isValidModel(makeId, modelId) => Good(Some(MakeModelParameters(makeId, Some(modelId), modelLineId = None, index))) case (Some(Good(modelLineId)), Some(makeId)) if modelLineId < 0 && taxonomyService.isValidModelLine(makeId, -1 * modelLineId) => Good(Some(MakeModelParameters(makeId, modelId = None, modelLineId = Some(-1 * modelLineId), index))) case (Some(Good(modelOrModelLineId)), Some(makeId)) => Bad(One(IdNotFound(modelIdKeyName, modelOrModelLineId))) case (Some(Good(modelId)), None) if modelId >= 0 => Bad(One(IdInvalid(modelIdKeyName, "Model Id without Make Id"))) case (Some(Good(modelLineId)), None) if modelLineId < 0 => Bad(One(IdInvalid(modelIdKeyName, "Model Line Id without Make Id"))) case (None, Some(makeId)) => Good(Some(MakeModelParameters(makeId, None, None, index))) // case (None, None) => Good(None) // case (Some(Bad(invalidParams)), _) => Bad(One(invalidParams)) }
我们评论了最后两行,但编译器没有发出警告,我们也没有禁用任何检查.
根据Jason Zaugg对SI-9232的评论,卫兵目前似乎只是禁用详尽检查(或几乎如此).如果这是原因,移动分支内的保护条件(例如case (Some(Good(modelId)), None) => if (modelId >= 0) ... else ...
)应该有所帮助.其他相关问题:https://issues.scala-lang.org/browse/SI-5365,https : //issues.scala-lang.org/browse/SI-7631.