2017-07-05 146 views
0

使用下面的代碼,new Box(10), new Box("20")對我很好。但對於new Box(Seq(20)), new Box(Seq("20"))), 我想弄清楚的序列類型參數的類型,這樣我就可以打印序列[INT],SEQ [字符串]如何確定嵌套類型參數的類型參數

 @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()) 

    } 

回答

1

新的正確的方式做到這一點是使用TypeTag代替ClassTag

def foo[T : TypeTag](data: T) = typeOf[T] match { 
    case t if t =:= typeOf[Int] => println("Int") 
    case t if t =:= typeOf[String] => println("String") 
    case t if t <:< typeOf[Seq[String]] => println("Seq[String]") 
    // etc 
} 

(如果你只是想打印出來,你可以也這樣做:

def foo[T : TypeTag](data: T) = println(typeOf[T])` 

它的SA我的東西,但處理所有類型,不只是你能想到的。

+0

感謝@dima的答案! – Tom

1

@dima的答案很優雅,我會提出另一種方法來逐步檢測類型參數。

@Test 
    def testTypeTag1(): Unit = { 
    class Box[T: TypeTag](val data: T) { 
     def printTypeParameter() = { 
     typeOf[T] match { 
      case t if t =:= typeOf[Int] => println("Int") 
      case t if t =:= typeOf[String] => println("String") 
      case t if t <:< typeOf[Seq[Any]] => { 
      val TypeRef(_, _, Seq(elementType)) = typeOf[T] 
      elementType match { 
       case t if t =:= typeOf[Int] =>println("Seq[Int]") 
       case t if t =:= typeOf[String] =>println("Seq[String]") 
       case _=>println("Seq[Unknown]") 
      } 
      } 
      case _ => println("Unknown") 
     } 
     } 
    } 
    val boxes = Seq(new Box(10), new Box("20"), new Box(Seq(20)), new Box(Seq("20"))) 
    boxes.foreach(_.printTypeParameter()) 
    }