我们有一些模式匹配代码,我们希望编译器警告我们匹配不是详尽的,但我们没有得到。是否存在无法进行详尽检查的情况?

例如我们的例子(使用 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's comment on 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 .


评论关闭
IT干货网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!