Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It's been awhile since I dug through the source code for Scala in a quest to answer the same question... but the short answer, as I recall -</p> <p>Manifest is a cheat code to allow the COMPILER to get around Type erasure (it isn't being used at runtime). It causes multiple code paths to be generated at compile time for the possible input types matching the manifest.</p> <p>The Manifest is resolved implicitly, but if there is any ambiguity at compile time about what the Manifest type is, the compiler WILL stop.</p> <p>With a copy of a <code>Manifest</code> you have a few things available. The main things you typically want is either the <code>java.lang.Class</code> that was erased via <code>erasure</code>:</p> <pre><code>class BoundedManifest[T &lt;: Any : Manifest](value: T) { val m = manifest[T] m.erasure.toString match { case "class java.lang.String" =&gt; println("String") case "double" | "int" =&gt; println("Numeric value.") case x =&gt; println("WTF is a '%s'?".format(x)) } } class ImplicitManifest[T &lt;: Any](value: T)(implicit m: Manifest[T]) { m.erasure.toString match { case "class java.lang.String" =&gt; println("String") case "double" | "int" =&gt; println("Numeric value.") case x =&gt; println("WTF is a '%s'?".format(x)) } } new BoundedManifest("Foo Bar!") // String new BoundedManifest(5) // Numeric value. new BoundedManifest(5.2) // Numeric value. new BoundedManifest(BigDecimal("8.62234525")) // WTF is a 'class scala.math.BigDecimal'? new ImplicitManifest("Foo Bar!") // String new ImplicitManifest(5) // Numeric value. new ImplicitManifest(5.2) // Numeric value. new ImplicitManifest(BigDecimal("8.62234525")) // WTF is a 'class scala.math.BigDecimal'? </code></pre> <p>This is a rather wonky example but shows what is going on. I ran that for the output as well FWIW on Scala 2.8.</p> <p>The <code>[T ... : Manifest]</code> boundary is new in Scala 2.8... you used to have to grab the manifest implicitly as shown in <code>ImplicitManifest</code>. You don't actually GET a copy of the Manifest. But you can fetch one inside your code by saying <code>val m = manifest[T]</code> ... <code>manifest[_]</code> is defined on <code>Predef</code> and demonstrably will find the proper manifest type inside a boundaried block.</p> <p>The other two major items you get from a <code>Manifest</code> is <code>&lt;:&lt;</code> and <code>&gt;:&gt;</code> which test subtype/supertype of one manifest versus another. If I recall correctly these are VERY naive implementation wise and don't always match but I have a bunch of production code using them to test against a few possible erased inputs. A simple example of checking against another manifest:</p> <pre><code>class BoundedManifestCheck[T &lt;: Any : Manifest](value: T) { val m = manifest[T] if (m &lt;:&lt; manifest[AnyVal]) { println("AnyVal (primitive)") } else if (m &lt;:&lt; manifest[AnyRef]) { println("AnyRef") } else { println("Not sure what the base type of manifest '%s' is.".format(m.erasure)) } } new BoundedManifestCheck("Foo Bar!") // AnyRef new BoundedManifestCheck(5) // AnyVal (primitive) new BoundedManifestCheck(5.2) // AnyVal (primitive) new BoundedManifestCheck(BigDecimal("8.62234525")) // AnyRef </code></pre> <p>Jorge Ortiz has a great blog post (albeit old) on this: <a href="http://www.scala-blogs.org/2008/10/manifests-reified-types.html" rel="noreferrer">http://www.scala-blogs.org/2008/10/manifests-reified-types.html</a></p> <p><strong>EDIT</strong>:</p> <p>You can actually see what Scala is doing by asking it to print out the results of the erasure compiler phase.</p> <p>Running, on my last example above <code>scala -Xprint:erasure test.scala</code> produces the following result:</p> <pre><code>final class Main extends java.lang.Object with ScalaObject { def this(): object Main = { Main.super.this(); () }; def main(argv: Array[java.lang.String]): Unit = { val args: Array[java.lang.String] = argv; { final class $anon extends java.lang.Object { def this(): anonymous class $anon = { $anon.super.this(); () }; class BoundedManifestCheck extends java.lang.Object with ScalaObject { &lt;paramaccessor&gt; private[this] val value: java.lang.Object = _; implicit &lt;paramaccessor&gt; private[this] val evidence$1: scala.reflect.Manifest = _; def this($outer: anonymous class $anon, value: java.lang.Object, evidence$1: scala.reflect.Manifest): BoundedManifestCheck = { BoundedManifestCheck.super.this(); () }; private[this] val m: scala.reflect.Manifest = scala.this.Predef.manifest(BoundedManifestCheck.this.evidence$1); &lt;stable&gt; &lt;accessor&gt; def m(): scala.reflect.Manifest = BoundedManifestCheck.this.m; if (BoundedManifestCheck.this.m().&lt;:&lt;(scala.this.Predef.manifest(reflect.this.Manifest.AnyVal()))) scala.this.Predef.println("AnyVal (primitive)") else if (BoundedManifestCheck.this.m().&lt;:&lt;(scala.this.Predef.manifest(reflect.this.Manifest.Object()))) scala.this.Predef.println("AnyRef") else scala.this.Predef.println(scala.this.Predef.augmentString("Not sure what the base type of manifest '%s' is.").format(scala.this.Predef.genericWrapArray(Array[java.lang.Object]{BoundedManifestCheck.this.m().erasure()}))); protected &lt;synthetic&gt; &lt;paramaccessor&gt; val $outer: anonymous class $anon = _; &lt;synthetic&gt; &lt;stable&gt; def Main$$anon$BoundedManifestCheck$$$outer(): anonymous class $anon = BoundedManifestCheck.this.$outer }; new BoundedManifestCheck($anon.this, "Foo Bar!", reflect.this.Manifest.classType(classOf[java.lang.String])); new BoundedManifestCheck($anon.this, scala.Int.box(5), reflect.this.Manifest.Int()); new BoundedManifestCheck($anon.this, scala.Double.box(5.2), reflect.this.Manifest.Double()); new BoundedManifestCheck($anon.this, scala.package.BigDecimal().apply("8.62234525"), reflect.this.Manifest.classType(classOf[scala.math.BigDecimal])) }; { new anonymous class $anon(); () } } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload