在 REPL 中,我写下了以下示例反射 - 类型标签和清单 http://docs.scala-lang.org/overviews/reflection/typetags-manifests.html.
我对之间的区别感到困惑WeakTypeTag
and TypeTag
.
scala> import scala.reflect.runtime.universe._
import scala.reflect.runtime.universe._
TypeTag
scala> def paramInfo[T](x: T)(implicit tag: TypeTag[T]): Unit = {
| val targs = tag.tpe match { case TypeRef(_, _, args) => args }
| println(s"type tag of $x has type arguments $targs")
| }
paramInfo: [T](x: T)(implicit tag: reflect.runtime.universe.TypeTag[T])Unit
弱类型标签
scala> def weakParamInfo[T](x: T)(implicit tag: WeakTypeTag[T]): Unit = {
| val targs = tag.tpe match { case TypeRef(_, _, args) => args }
| println(s"type tag of $x has type arguments $targs")
| }
weakParamInfo: [T](x: T)(implicit tag: reflect.runtime.universe.WeakTypeTag[T])Unit
运行一个简单的、非详尽的示例
scala> paramInfo2(List(1,2,3))
type of List(1, 2, 3) has type arguments List(Int)
scala> weakParamInfo(List(1,2,3)
| )
type tag of List(1, 2, 3) has type arguments List(Int)
他们之间有什么区别?