// =「 あとでここの型情報を使うから覚えておいてね」 def paramInfo[T](x: T)(implicit tag: TypeTag[T]) = { val targs = tag.tpe match { case TypeRef(_, _, args) => args } println(s"type of $x has type arguments $targs") } scala> paramInfo(42) type of 42 has type arguments List() scala> paramInfo(List(1, 2)) type of List(1, 2) has type arguments List(Int)
val targs = tag.tpe match { case TypeRef(_, _, args) => args } println(s"type of $x has type arguments $targs") } ↓context bound を使うと略記できる def paramInfo[T: TypeTag](x: T) = { val targs = typeOf[T] match { case TypeRef(_, _, args) => args } println(s"type of $x has type arguments $targs") }