Scala 匹配错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5000376/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 02:48:57  来源:igfitidea点击:

Scala match error

scala

提问by Andriy Drozdyuk

I tried to replace my isInstanceOf check with a match, but it does not work.

我试图用匹配替换我的 isInstanceOf 检查,但它不起作用。

In my method I do a check for a tree node - if it is a leaf - I want to return it inside a Vector right away, if not - I continue with the method.

在我的方法中,我检查树节点 - 如果它是叶子 - 我想立即将它返回到 Vector 中,如果不是 - 我继续使用该方法。

So originally I had:

所以最初我有:

    //code here
    if (common.isInstanceOf[LeafNode]) {
        return Vector(common.asInstanceOf[LeafNode].data)
    }
    //code here

then I tried to replace it with:

然后我尝试将其替换为:

    //code here
     common match {
        case leaf: LeafNode => return Vector(leaf.data)
    }
    //code here

but I get scala.MatchError.

但我得到了 scala.MatchError。

回答by Lachlan

You're getting a MatchErrorin the case where your commonis not a LeafNode. Your ifand matchexpressions are not equivalent. I think the most direct way to make them equivalent is:

你得到MatchError的情况下你common是不是LeafNode。您的ifmatch表达式不等价。我认为使它们等效的最直接方法是:

common match {
  case leaf: LeafNode => return Vector(leaf.data)
  case _ =>
}

But I'd recommend looking at the whole code block and working out are more functional way to do this job. That is, without the returnin the middle. Remember that match is an expression, so that something like this may be possible:

但我建议查看整个代码块并找出更实用的方法来完成这项工作。也就是说,没有return中间。记住 match 是一个表达式,所以这样的事情可能是可能的:

def foo = {
  //code here
  common match {
    case leaf: LeafNode => Vector(leaf.data)
    case notLeaf: Branch => //code here
  }
}

回答by Tom Crockett

The problem is that the set of cases in your matchblock is not exhaustive; if commonis anything but a LeafNode, a MatchErrorwill be thrown. You can solve this by having a catch-all case like so:

问题是您的match区块中的案例集并不详尽;如果common不是 a LeafNodeMatchError则将抛出 a 。你可以通过像这样一个包罗万象的案例来解决这个问题:

common match {
  case leaf: LeafNode => return Vector(leaf.data)
  ... // other cases
  case other => ... // what to do if nothing else matches
}

This is analogous to the defaultcase in a Java switch statement. The othercase is called an "irrefutable pattern" because it has no characteristics; it doesn't require a particular type or constructor, so it will always match anything that falls through to it. The name of the variable doesn't have to be other, it can be anything you want, or even _... in fact you don't need to bind a new variable here since it will be identical to common.

这类似于defaultJava switch 语句中的情况。这个other案子因为没有特点,所以被称为“无可辩驳的格局”;它不需要特定的类型或构造函数,所以它总是会匹配任何落入它的东西。变量的名称不必是other,它可以是您想要的任何名称,甚至_……实际上您不需要在此处绑定新变量,因为它将与common.

On a point of style, it's generally bad form to put return statements inside of a matchblock; the entire block is an expression which evaluates to one of its cases, so just return the entire expression. Also, you don't need to use the returnkeyword at all, as the last expression in a function definition will be used as the result.

就风格而言,将 return 语句放在match块中通常是不好的形式;整个块是一个表达式,其计算结果为其中一种情况,因此只需返回整个表达式。此外,您根本不需要使用return关键字,因为函数定义中的最后一个表达式将用作结果。