sbt compile time warning: non-variable type argume

2020-05-05 18:48发布

问题:

My sbt is showing a warning message

non-variable type argument String in type pattern List[String] (the underlying of List[String]) is unchecked since it is eliminated by erasure

I tried the answer given in the link (first solution)

Erasure elimination in scala : non-variable type argument is unchecked since it is eliminated by erasure

Here is my code

case class ListStrings(values:scala.List[String]) { }
def matchValue(value: Any) = { 
  value match {      
    case ListStrings(xs) => 
      val userList = xs
    case _ => log.error("unknown value")
  }
}
val list: List[String] = List("6","7","8")

matchValue(list)

I am getting "unknown value" as an output why its not matching ? what i am missing here?

回答1:

Because you passed list instead of ListStrings(list)



回答2:

First with this it works:

val list = ListStrings(List("6" ,"7","8"))

So you see the problem.

To achieve this, the simplest is maybe to add an apply constructor:

object ListStrings {
  def apply(values: String*): ListStrings = ListStrings(values.toList)
}

Now you can call it like:

val list = ListStrings("6" ,"7","8")


标签: scala sbt