scala typetag java,Scala中TypeTags和Manifests的用法

Scala运行在JVM上,在JVM上有一种机制叫做类型擦除(type eraser)。类型擦除是说:在语言的编译阶段程序中所携带的泛型信息都会被擦除,最终生成的class文件中是不包含类型信息的。所以scala 2.8中引入了Manifests类来解决构建Array时遇到的一些问题。然而引入的Manifests并不完美,对于路径依赖类型,它拿到的信息是不精确的。所以后续引入了TypeTags。

展示一下各自的用法。看一个例子,利用manifest获取类型信息。

scala> class A[T]

scala> manifest[List[A[Int]]]

res1: Manifest[List[A[Int]]] = scala.collection.immutable.List[A[Int]]

manifest定义在Predef中,直接使用即可。与manifest功能类似的一个方法是classManifest,

scala> classManifest[List[A[Int]]]

warning: there was one deprecation warning; re-run with -deprecation for details

res2: ClassManifest[List[A[Int]]] = scala.collection.immutable.List[A[Int]]

两个方法都可以拿到相应传入的类型信息,但是观察返回结果:res1的类型是Manifest[List[A[Int]]],res2的类型是ClassManifest[List[A[Int]]],两个东西还是有一些区别。接着再看一个例子:

scala> manifest[List[A[_]]]

res3: scala.reflect.Manifest[List[A[_]]] = scala.collection.immutable.List[A[_ <: any>

scala> classManifest[List[A[_]]]

warning: there was one deprecation warning; re-run with -deprecation for details

res4: scala.reflect.ClassTag[List[A[_]]] = scala.collection.immutable.List[A[>]]

在这个例子中manifest会返回所有的类型信息,而classManifest返回了List[A]两层类型,丢失最内部的泛型信息。通过看源码注释对ClassManifest的解释,针对高阶类型它可能不会给出精确的类型信息。所以如果想要得到精确地类型信息使用manifest。

通常Manifest会以隐式参数和上下文绑定的形式使用。

def method[T](arg: T)(implicit m: Manifest[T]) = m //implicit parameter

def method[T: Manifest](arg: T) = m //context bound style

那么,有了Manifest,还要TypeTag干吗?

scala> class Foo { class Bar}

defined class Foo

scala> def m(f: Foo)(b: f.Bar)(implicit t: Manifest[f.Bar]) = t

m: (f: Foo)(b: f.Bar)(implicit t: scala.reflect.Manifest[f.Bar])scala.reflect.Manifest[f.Bar]

scala> val f = new Foo; val b = new f.Bar

f: Foo = Foo@6fe29d36

b: f.Bar = Foo$Bar@6bf7d9d

scala> val ff = new Foo; val bb = new ff.Bar

ff: Foo = Foo@2fdb85b8

bb: ff.Bar = Foo$Bar@75280b93

scala> m(f)(b)

res20: scala.reflect.Manifest[f.Bar] = Foo@6fe29d36.type#Foo$Bar

scala> m(ff)(bb)

res21: scala.reflect.Manifest[ff.Bar] = Foo@2fdb85b8.type#Foo$Bar

scala> res20 == res21 //not expected

res23: Boolean = true

很明显f.Bar和ff.Bar是不相同的类型。所以,scala2.10 引入了TypeTags来解决上述问题,TypeTag可以看成是Manifest的升级版。

def m2(f: Foo)(b: f.Bar)(implicit ev: TypeTag[f.Bar]) = ev

val ev3 = m2(f1)(b1)

val ev4 = m2(f2)(b2)

ev3 == ev4 //false

和TypeTag一同引入的还有ClassTag和WeakTypeTag。ClassTag和ClassManifest一样,也不会返回完整的类型信息。但是它好classManifiest还是有区别的。

scala> classTag[List[_]]

res9: scala.reflect.ClassTag[List[_]] = scala.collection.immutable.List

scala> classManifest[List[_]]

warning: there was one deprecation warning; re-run with -deprecation for details

res10: scala.reflect.ClassTag[List[_]] = scala.collection.immutable.List[>]

scala> classTag[List[List[_]]]

res11: scala.reflect.ClassTag[List[List[_]]] = scala.collection.immutable.List

scala> classManifest[List[List[_]]]

warning: there was one deprecation warning; re-run with -deprecation for details

res12: scala.reflect.ClassTag[List[List[_]]] = scala.collection.immutable.List[scala.collection.immutable.List[>]]

classTag只拿到了基础类型信息,没有尝试去拿任何泛型信息,而classManifest拿到了所有已知的类型信息。WeakTypeTag相较TypeTag可以处理带有泛型的参数。看下边的例子:

scala> :pas

// Entering paste mode (ctrl-D to finish)

def weakParamInfo[T](x: T)(implicit tag: WeakTypeTag[T]): Unit = {

val targs = tag.tpe match { case TypeRef(_, _, args) => args }

println(s"type of $x has type arguments $targs")

}

// Exiting paste mode, now interpreting.

weakParamInfo: [T](x: T)(implicit tag: reflect.runtime.universe.WeakTypeTag[T])Unit

scala> def foo[T] = weakParamInfo(List[T]())

foo: [T]=> Unit

scala> foo[Int]

type of List() has type arguments List(T)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值