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

How to fix compiler warning on scala.xml.Elem

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

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

如何解决?

您可以使用 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 => }