清单[T]是类型T的晦涩描述符。它的支持用途是作为类实例提供对类型擦除的访问,如果类在编译时未知,这是创建原生数组所必需的。
A Manifest[T] is an opaque descriptor for type T. Its supported use is to give access to the erasure of the type as a Class instance, as is necessary for the creation of native Arrays if the class is not known at compile time.
类型关系运算符<:<和=:=应仅被视为近似值,因为类型一致性的许多方面在清单中尚未得到充分体现。
The type-relation operators <:< and =:= should be considered approximations only, as there are numerous aspects of type conformance which are not yet adequately represented in manifests.
scala> def arr[T] = new Array[T](0)
<console>:11: error: cannot find class tag for element type T
def arr[T] = new Array[T](0)
^
scala> def arr[T](implicit m: Manifest[T]) = new Array[T](0)
arr: [T](implicit m: Manifest[T])Array[T]
scala> def arr[T: Manifest] = new Array[T](0)
arr: [T](implicit evidence$1: Manifest[T])Array[T]
scala> def isApproxSubType[T: Manifest, U: Manifest] = manifest[T] <:< manifest[U]
warning: there was one deprecation warning; re-run with -deprecation for details
isApproxSubType: [T, U](implicit evidence$1: Manifest[T], implicit evidence$2: Manifest[U])Boolean
scala> isApproxSubType[List[String], List[AnyRef]]
res5: Boolean = true
scala> isApproxSubType[List[String], List[Int]]
res6: Boolean = false
scala> def methods[T: Manifest] = manifest[T].runtimeClass.getMethods
methods: [T](implicit evidence$1: Manifest[T])Array[java.lang.reflect.Method]
scala> def retType[T: Manifest](name: String) =
methods[T] find (_.getName == name) map (_.getGenericReturnType)
| retType: [T](name: String)(implicit evidence$1: Manifest[T])Option[java.lang.reflect.Type]