Manifest和TypeTag是要解决什么问题?
 
As with other JVM languages, Scala’s types are erased at compile time. This means that if you were to inspect the runtime type of some instance, you might not have access to all type information that the Scala compiler has available at compile time.
 
由于类型擦除,在编译期存在的类型信息,在编译后,在runtime就丢失了。为了在runtime获得编译期得到的类型信息,需要额外传递信息,所以需要Manifest和TypeTag携带这些信息。
Manifest和TypeTag把这些类型信息从compile time 携带到 runtime.
 
A TypeTag[T] encapsulates the runtime type representation of some type T.所以,只要使用正确的TypeTag[T],就能在运行期获取T的类型信息。
 
Manifest是scala2.8引入的一个特质,用于编译器在运行时获取泛型类型的信息。在JVM上,泛型参数类型T在运行时是被“擦拭”掉的,编译器把T当作Object来对待,所以T的具体信息是无法得到的;为了使得在运行时得到T的信息,scala需要额外通过Manifest来存储T的信息,并作为参数用在方法的运行时上下文
 
scala> def max[T : Comparator] (a:T, b:T) = { … }
 
上面的类型参数声明 T : Comparator 就表示存在一个 Comparator[T]类型的隐式值。
 
 
TypeTag 在import scala.reflect.runtime.universe._路径下
它用来替代Manifest。因为Manifest不能识别path依赖的类型。
class Foo{class Bar} 中的 foo1.bar1 和foo2.bar2, Manifest就会识别为同一类型。而TypeTag不会
 
而且TypeTag可以用来检查类型参数,使用typeOf[T]
 
import scala.reflect.runtime.universe._

def meth[A : TypeTag](xs: List[A]) = typeOf[A] match {
case t if t =:= typeOf[String] => "list of strings"
case t if t <:< typeOf[Foo] => "list of foos"} scala> meth(List("string"))
res67: String = list of strings scala> meth(List(new Foo))
res68: String = list of foos
上边的typeOf[A]在传入参数为List("String")时,得到结果是java.lang.String
typeOf[T]接受一个类型为TypeTag[T]的隐式参数,所以编译器生成的TypeTag隐式参数会被传给typeOf[T]
 
 
有哪些种TypeTag?
 

There exist three different types of TypeTags:

  1. scala.reflect.api.TypeTags#TypeTag. A full type descriptor of a Scala type. For example, a TypeTag[List[String]] contains all type information, in this case, of typescala.List[String].

  2. scala.reflect.ClassTag. A partial type descriptor of a Scala type. For example, aClassTag[List[String]] contains only the erased class type information, in this case, of type scala.collection.immutable.ListClassTags provide access only to the runtime class of a type. Analogous to scala.reflect.ClassManifest.

  3. scala.reflect.api.TypeTags#WeakTypeTag. A type descriptor for abstract types (see corresponding subsection below).

如何在运行时获取叁数类型的信息?
 
首先通过Manifest和TypeTag获取的信息是针对参数类型的,而不是参数。所以一个TypeTag对象是跟一个类型相关的,而不是和一个具体的对象相关的。
那么问题是:
1. 如何让编译器知道哪个类型参数需要生成TypeTag?显式地使用隐式参数,或者在声明泛型参数时使用context bound,这编译器自动生成隐式参数。
2. 如何在代码体中获取TypeTag? 当显式使用隐式参数时,可以直接使用这个隐式参数;当使用context bounds时,使用接受相关隐式参数的方法,比如typeOf。
 
 
三种方法:
  1. via the Methods typeTagclassTag, or weakTypeTag
  2. Using an Implicit Parameter of Type TypeTag[T]ClassTag[T], orWeakTypeTag[T]
  3. Using a Context bound of a Type Parameter
见http://docs.scala-lang.org/overviews/reflection/typetags-manifests.html
 
 
 
 
 
 

Manifest 与TypeTag的更多相关文章

  1. scala高级内容(二) - Implicit

    一. Implicit关键字 隐士转换 (1)隐士转换函数:用implicit修饰的,只有一个参数的函数.他会被自动执行,来把一个值转换成另一个 class RichFile(val f:File){ ...

  2. shapeless官方指南翻译写在前面

    目录 前言 Shapeless简介 The Type Astronaut's Guide to Shapeless简介 总结 一.前言        在我的2016,感恩.乐观.努力一文中,说2017 ...

  3. Flink - TypeInformation

    Flink 自己创建一套独立的类型系统, 参考, https://ci.apache.org/projects/flink/flink-docs-release-0.10/internals/type ...

  4. Scala入门到精通——第二十四节 高级类型 (三)

    作者:摆摆少年梦 视频地址:http://blog.csdn.net/wsscy2004/article/details/38440247 本节主要内容 Type Specialization Man ...

  5. Scala 深入浅出实战经典 第46讲: ClassTag 、Manifest、ClasMainifest TagType实战

    王家林亲授<DT大数据梦工厂>大数据实战视频 Scala 深入浅出实战经典(1-64讲)完整视频.PPT.代码下载:百度云盘:http://pan.baidu.com/s/1c0noOt6 ...

  6. HTML 5 应用程序缓存manifest

    什么是应用程序缓存(Application Cache)? HTML5 引入了应用程序缓存,这意味着 web 应用可进行缓存,并可在没有因特网连接时进行访问. 应用程序缓存为应用带来三个优势: 离线浏 ...

  7. HTML5之应用缓存---manifest---缓存使用----Web前端manifest缓存

    相信来查这一类问题的都是遇到问题或者是初学者吧! 没关系相信你认真看过之后就会知道明白的 这是HTML5新加的特性 HTML5 引入了应用程序缓存,这意味着 web 应用可进行缓存,并可在没有因特网连 ...

  8. Scala Reflection - Mirrors,ClassTag,TypeTag and WeakTypeTag

    反射reflection是程序对自身的检查.验证甚至代码修改功能.反射可以通过它的Reify功能来实时自动构建生成静态的Scala实例如:类(class).方法(method).表达式(express ...

  9. gulp rev manifest 添加目录前缀

    gulp-rev 生成的manifest默认为: "index.css": "index.css?v=04aff97a7b", 为避免同名文件覆盖版本号,对替换 ...

随机推荐

  1. 【ROW_NUMBER 函数(Transact-SQL)】

    [ROW_NUMBER 函数(Transact-SQL)]返回结果集分区内行的序列号,每个分区的第一行从 1 开始. 注释: ROW_NUMBER() OVER (PARTITION BY COL1 ...

  2. mvc Web api 如何在控制器中调用

    关于如何调用 mvc Web api 的方法,网上一搜就是一大把,基本都是在前台jq中调用的,但是如何在后台调用呢? 本楼主做了一下测试,仅供参考. 先写一个简单的api,如下:[域1] namesp ...

  3. <转载>批处理之FOR语句祥解

    批处理之FOR语句祥解 FOR这条命令基本上都被用来处理文本,但还有其他一些好用的功能! 看看他的基本格式(这里我引用的是批处理中的格式,直接在命令行只需要一个%号) FOR 参数 %%变量名 IN ...

  4. 在SQL 2012中使用和Oracle 一样的序列

    使用过Oracle的都知道,Oracle中的自增是靠序列来完成的,在一定程度上蛮方便的.现在SQL 2012中也有序列了.来看看怎么做的吧! SQL Server 现在将序列当成一个对象来实现,创建一 ...

  5. WCF学习笔记 -- 基本概念

    WCF是实现WebService的一种微软提出的技术,整合了.Remote, .NET及ASP.NET服务的一种框架.是Windows Communication Foundation的缩写.WebS ...

  6. Oracle创建新用户

    1.以DBA身份登录 $ sqlplus sys/eastcom@ORCL as sysdba(在命令窗口下) 也可以使用PL/SQL 2.创建临时表空间 create temporary table ...

  7. 《RedHatLinux系统修复视频(通过本地镜像)》

    此视频的测试环境: win7下安装的VMware    redhatlinux6.3系统的修复     以删掉kernel系统引导故障重新安装kernel为例 基于本地镜像来对系统进行修复 如疑问可留 ...

  8. SQL 存储过程加事务的使用

    create proc USP_CUTTING_TATABLET_PULL_FINISH ( @name NVARCHAR(20) ) as SET XACT_ABORT ON--设置全盘回滚 BEG ...

  9. Brackets - 又一款牛x的WEB开发编辑器

    Brackets官网下载: http://brackets.io/ Adobe Brackets是由Adobe主导开发一款主打web开发的编辑器. 是继TextMate,Sublime Text这两个 ...

  10. 一段画对角线的canvas代码,之前没有写过canvas代码,现在记录下来

    <canvas id="other" style="width:320px;height:320px;"></canvas> var o ...