【问题标题】:Unreachable code in scala?scala中无法访问的代码?
【发布时间】:2014-06-10 19:09:03
【问题描述】:

由于某种原因,以下代码无法访问。我不明白为什么我的代码永远不会被访问,因为这是一个简单的模式匹配。这里是:

type Occurrences = List[(Char, Int)]

def combinations(occurrences: Occurrences): List[Occurrences] = occurrences match{
  case Nil => Nil
  case List() => List()
  case x => List(x)
  case x::xs => combinations(List((x._1,x._2 - 1))) ::: combinations(xs)
}

此算法旨在提取给定列表的所有子列表。

【问题讨论】:

    标签: scala pattern-matching unreachable-code


    【解决方案1】:

    case x => List(x) 匹配任何内容。看起来您想匹配一个 1 元素列表,以便您可以使用:

    case l@List(_) => List(l)
    

    【讨论】:

      【解决方案2】:
      scala> Nil == List()
      res0: Boolean = true
      

      你认为List() 是什么?

      顺便说一句,错误消息确实告诉您问题所在:

      scala> def combinations(occurrences: Occurrences): List[Occurrences] = occurrences match{
           |   case Nil => Nil
           |   case List() => List()
           |   case x => List(x)
           |   case x::xs => combinations(List((x._1,x._2 - 1))) ::: combinations(xs)
           | }
      <console>:11: warning: patterns after a variable pattern cannot match (SLS 8.1.1)
               case x => List(x)
                    ^
      <console>:12: warning: unreachable code due to variable pattern 'x' on line 11
               case x::xs => combinations(List((x._1,x._2 - 1))) ::: combinations(xs)
                                                                 ^
      <console>:12: warning: unreachable code
               case x::xs => combinations(List((x._1,x._2 - 1))) ::: combinations(xs)
                                                                 ^
      combinations: (occurrences: Occurrences)List[Occurrences]
      

      【讨论】:

        【解决方案3】:

        我是一个 Scala 新手,我遇到了类似的问题,并且对编译器给出的警告感到困惑。如果其他人遇到同样的事情,这就是让我感到困惑的地方以及我的解决方案。

        我的编译器给了我一个大致这样的警告(但我用 OP 的代码代替了我的代码):

        [warn] path/to/file.scala:123: unreachable code
        case x::xs => combinations(List((x._1,x._2 - 1))) ::: combinations(xs)
                                                           ^
        

        编译器指向您最后一个 case 语句中的一个符号,实际上(正如其他人所提到的)问题在于您的 List() case 语句。起初我误以为这意味着编译器打印的 case 语句存在问题。 (在我的例子中,我认为我用错了一个 '::'。)

        编译器消息的意思是该语句不可达,与它指向的字符无关。正如其他人所提到的,该语句无法访问的原因是 List() 语句是一个包罗万象的语句,并且匹配不会流向其余的情况。

        要解决此问题,请确保逻辑可以贯穿您的所有案例,并且不会陷入包罗万象的困境。即从最具体到最不具体。我还尝试按元素数量匹配列表,这是我的解决方案 [编译并工作,没有警告!]:

        def myFunction(myList: List[MyType]) = myList match {
           case x1 :: x2 :: xs => // match 2+ elems
           case x :: xs => // match 1+ elems
           case Nil => // match 0 elems
        

        但是,如果我重新排列前两行,我会再次收到无法访问的代码警告,因为匹配 2+ 的情况比 1+ 的情况更具体 [不起作用,以及无法访问的代码警告!]:

        def myFunction(myList: List[MyType]) = myList match {
           case x :: xs => // match 1+ elems
           case x1 :: x2 :: xs => // match 2+ elems
           case Nil => // match 0 elems
        

        【讨论】:

          【解决方案4】:

          我猜你想用case x =&gt; List(x) 匹配一个元素。这匹配任何东西。
          请改用case x :: Nil =&gt; List(x)

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多