次のコードでは、new Box(10), new Box("20")
がうまく動作します。しかしnew Box(Seq(20)), new Box(Seq("20")))
、 のために私は私が配列[のInt]を印刷することができるように、配列の型パラメータの種類を把握したいと思い、配列[文字列]入れ子型パラメータの型パラメータを調べる方法
@Test
def testClassTag(): Unit = {
class Box[T:ClassTag](val data: T) {
def printTypeParameter() = {
val tag = implicitly[ClassTag[T]].runtimeClass
tag match {
case _ if tag == classOf[Int] => println("Int")
case _ if tag == classOf[String] => println("String")
case _ if tag == classOf[Seq[_]] => println("Seq")
}
}
}
val boxes = Seq(new Box(10), new Box("20"), new Box(Seq(20)), new Box(Seq("20")))
boxes.foreach(_.printTypeParameter())
}
回答ありがとうございました! – Tom