Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala编译器说无法访问的代码,为什么?_Scala_Unreachable Code - Fatal编程技术网

Scala编译器说无法访问的代码,为什么?

Scala编译器说无法访问的代码,为什么?,scala,unreachable-code,Scala,Unreachable Code,我是Scala的新手。。。代码如下: def ack2(m: BigInt, n: BigInt): BigInt = { val z = BigInt(0) (m,n) match { case (z,_) => n+1 case (_,z) => ack2(m-1,1) // Compiler says unreachable code on the paren of ack2( case _

我是Scala的新手。。。代码如下:

  def ack2(m: BigInt, n: BigInt): BigInt = {
      val z = BigInt(0)
      (m,n) match {
          case (z,_) => n+1
          case (_,z) => ack2(m-1,1) // Compiler says unreachable code on the paren of ack2(
          case _ => ack2(m-1, ack2(m, n-1)) // Compiler says unreachable code on the paren of ack2(
      }
  }
我在努力理解。。。为什么会出现这样的错误

注意:我正在使用
Scala Eclipse插件2.8.0.r21376-b20100408034031 ch.epfl.lamp.sdt.feature.group

模式匹配中的z不是指您在外部声明的z,它引入了一个新的变量绑定。因此,第一种情况将匹配每一个可能的对(将z绑定到对的第一个元素并丢弃第二个元素),而其他情况将永远不会达到

如果将模式中的
z
替换为

`z`

它将引用现有的z,而不是引入新的绑定,因此它将按照您的意愿工作。如果您不喜欢带有反勾号的语法,也可以将z重命名为z。

好吧,您回答了我的问题。。。我也应该问一下如何最好地修复它:)我可以将它改为case u,如果m==0=>n+1;如果n==0=>ack2(m-1,1),那么这是唯一的方法吗?我不能在案例结束后直接将0放在括号中,因为它抱怨它不是一个BigInt。我不能将BigInt(0)放入parens,因为它抱怨它不是case类构造函数