是否可以在Scala 2.11.2中确定Seq[A]
的类型?
例如:
val fruit = List("apples", "oranges", "pears")
val nums = List(1, 2, 3, 4)
我想打印这样的Seq类型:
scala> def printType[A](xs: Seq[A]): Unit = xs match {
| case x: List[String] => println("String")
| case y: List[Int] => println("Int")
| }
<console>:8: warning: non-variable type argument String in type pattern List[Str
ing] (the underlying of List[String]) is unchecked since it is eliminated by era
sure
case x: List[String] => println("String")
^
<console>:9: warning: non-variable type argument Int in type pattern List[Int] (
the underlying of List[Int]) is unchecked since it is eliminated by erasure
case y: List[Int] => println("Int")
^
<console>:9: warning: unreachable code
case y: List[Int] => println("Int")
^
printType: [A](xs: Seq[A])Unit
P.S。我是斯卡拉的新人。
更新
是否有不使用Reflection API的解决方案?
答案 0 :(得分:0)
您可以使用类型标记,请参阅此问题的已接受答案,以获取与您自己类似的详细示例:Scala: What is a TypeTag and how do I use it?