sbt compile time warning: non-variable type argument String in type pattern List[String]

谁说我不能喝 提交于 2020-01-22 02:30:08

问题


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")


来源:https://stackoverflow.com/questions/59158822/sbt-compile-time-warning-non-variable-type-argument-string-in-type-pattern-list

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!