gpt4 book ai didi

scala - Scala 2.11 模式匹配的详尽检查

转载 作者:行者123 更新时间:2023-12-04 11:03:38 27 4
gpt4 key购买 nike

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

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

关于scala - Scala 2.11 模式匹配的详尽检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34022160/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com