如何修复scala.xml.Elem上的编译器警告

时间:2015-06-03 18:15:20

标签: xml scala compiler-warnings

以下代码生成此编译警告:重复的案例参数或提取的序列应仅与序列通配符匹配(_ *)

import scala.xml.Elem
def matchElem(e: Elem) = e match { case <source/> => "match!" }

如何解决这个问题?

1 个答案:

答案 0 :(得分:3)

您可以使用scalac -Xlint:-stars-align,_来取消警告,该警告适用于this issue

您的功能看起来像-Xprint:typer

        def matchElem(e: scala.xml.Elem): String = e match {
          case scala.xml.Elem.unapplySeq(<unapply-selector>) <unapply> (_, "source", _, _) => "match!"
        }

回答你的问题:

$ scala -Xlint
Welcome to Scala version 2.11.6 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_45).
Type in expressions to have them evaluated.
Type :help for more information.

scala> import xml._
import xml._

scala> def matchElem(e: Elem) = e match { case Elem(_, "source", _, _, _*) => "match!" }
matchElem: (e: scala.xml.Elem)String

或在模式中嵌入序列通配符:

scala> <a/> match { case <a/> => }
<console>:8: warning: A repeated case parameter or extracted sequence should be matched only by a sequence wildcard (_*).
       <a/> match { case <a/> => }
                          ^

scala> <a/> match { case <a>{ ns @ _* }</a> if ns.isEmpty => }