A ClassTag[T] stores the erased class of a given type T, accessible via the runtimeClass field. This is particularly useful for instantiating Arrays whose element types are unknown at compile time.
ClassTags are a weaker special case of scala.reflect.api.TypeTags#TypeTags, in that they wrap only the runtime class of a given type, whereas a TypeTag contains all static type information. That is, ClassTags are constructed from knowing only the top-level class of a type, without necessarily knowing all of its argument types. This runtime information is enough for runtime Array creation.
scala> def mkArray[T : ClassTag](elems: T*) = Array[T](elems: _*)
mkArray: [T](elems: T*)(implicit evidence$1: scala.reflect.ClassTag[T])Array[T]
scala> mkArray(42, 13)
res0: Array[Int] = Array(42, 13)
res1: Array[String] = Array(Japan, Brazil, Germany)
See scala.reflect.api.TypeTags for more examples, or the Reflection Guide: TypeTags for more details.
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.
def arr[T] = new Array[T](0) // does not compile
def arr[T](implicit m: Manifest[T]) = new Array[T](0) // compiles
def arr[T: Manifest] = new Array[T](0) // shorthand for the preceding
// Methods manifest, classManifest, and optManifest are in [[scala.Predef]].
def isApproxSubType[T: Manifest, U: Manifest] = manifest[T] <:< manifest[U]
isApproxSubType[List[String], List[AnyRef]] // true
isApproxSubType[List[String], List[Int]] // false
def methods[T: ClassManifest] = classManifest[T].erasure.getMethods
def retType[T: ClassManifest](name: String) =
methods[T] find (_.getName == name) map (_.getGenericReturnType)
retType[Map[_, _]]("values") // Some(scala.collection.Iterable<B>)
A OptManifest[T] is an optional scala.reflect.Manifest.
It is either a Manifest or the value NoManifest.
A ClassManifest[T] is an opaque descriptor for type T. It is used by the compiler to preserve information necessary for instantiating Arrays in those cases where the element type is unknown at compile time.
The type-relation operators make an effort to present a more accurate picture than can be realized with erased types, but they should not be relied upon to give correct answers. In particular they are likely to be wrong when variance is involved or when a subtype has a different number of type arguments than a supertype.
(Since version 2.10.0) use scala.reflect.ClassTag instead
The object Manifest defines factory methods for manifests. It is intended for use by the compiler and should not be used in client code.
Make a java reflection object accessible, if it is not already and it is possible to do so. If a SecurityException is thrown in the attempt, it is caught and discarded.
ClassManifestFactory defines factory methods for manifests. It is intended for use by the compiler and should not be used in client code.
Unlike ClassManifest, this factory isn't annotated with a deprecation warning. This is done to prevent avalanches of deprecation warnings in the code that calls methods with manifests.
In a perfect world, we would just remove the @deprecated annotation from ClassManifest the object and then delete it in 2.11. After all, that object is explicitly marked as internal, so no one should use it. However a lot of existing libraries disregarded the Scaladoc that comes with ClassManifest, so we need to somehow nudge them into migrating prior to removing stuff out of the blue. Hence we've introduced this design decision as the lesser of two evils.
ManifestFactory defines factory methods for manifests. It is intended for use by the compiler and should not be used in client code.
Unlike Manifest, this factory isn't annotated with a deprecation warning. This is done to prevent avalanches of deprecation warnings in the code that calls methods with manifests. Why so complicated? Read up the comments for ClassManifestFactory.
Provides functions to encode and decode Scala symbolic names. Also provides some constants.
© 2002-2019 EPFL, with contributions from Lightbend.Licensed under the Apache License, Version 2.0.